|
Here is the actual snippet of the code.
I have "hard coded " the RegExp
RegExp = "[/\\w+/g]+";
text = " validate regular expression ";
text += RegExp;
qDebug() << text;
textDEBUG->append(text);
text = " validate inString ";
text += inString;
qDebug() << text;
textDEBUG->append(text);
QRegularExpression re(RegExp);
QRegularExpressionMatch match = re.match(inString);
if (match.hasMatch()) { text = " Has all match ";
QStringList result = match.capturedTexts();
text += result.at(0);
qDebug() << text;
textDEBUG->append(text);
return result.at(0);
Here is the relevant debug output
"START EditLine_RegExp...QString BT_Utility_Library::EditLine_RegExp_Ext(QString, QString, QTextEdit *, QTextEdit *)1321"
" instring text \t\n Waiting to connect to bluetoothd...\r\u001B[0;94m[bluetooth]\u001B[0m# \r\r\u001B[0;94m[bluetooth]\u001B[0m# \r \rAgent registered\n\u001B[0;94m[bluetooth]\u001B[0m# "
" regular expression \t\n (\\w+\\s:?)"
" validate regular expression [/\\w+/g]+"
" validate inString Waiting to connect to bluetoothd...\r\u001B[0;94m[bluetooth]\u001B[0m# \r\r\u001B[0;94m[bluetooth]\u001B[0m# \r \rAgent registered\n\u001B[0;94m[bluetooth]\u001B[0m# "
" Has all match Waiting"
10:12:17: /mnt/RAID_124/BT/BT_Oct23_BASE_/mdi/MDI exited with code 0
The expression matches ONLY the first word it finds.
My goal is to match ALL the words in the inString.
I am going to try one of the AI reg exp generators, but from experience
using them this RegExp MAY work....
|
|
|
|
|
|
Salvatore Terress wrote: learn more about using regular expression....RegExp = "[/\\w+/g]+";
Keep in mind that that form of a regular expression will be unlikely to work in any other regular expression interpreter.
Perl, javascript, C# and Java (perhaps others) all use the same rules for most of the basics for regex and that will not work with any of them.
For those that means the following
- Match A-Za-z0-9.
- Match a forward slash (redundant twice)
- Match a 'g'. Redundant with the word class match.
|
|
|
|
|
After some "RTFM" I came up with this code
if(inString.contains("Agent") & inString.contains("registered") )
{
text = "Match ";
}else
{
text = " No match ";
}
qDebug() << text;
textDEBUG->append(text);
The " contains " actually accepts reg expression and string too , so I am not sure how
to tell the difference.
But it does what I want it to do.
|
|
|
|
|
SOLVED
Thanks to all contributors who help to find the issue.
modified 29-Oct-23 10:49am.
|
|
|
|
|
At a guess, I would bet that textEditPtr_DEBUG may not be a valid object. In brief:
QTextEdit *textEditPtr_DEBUG = MWCCF->centralWidget()->findChild<QTextEdit *>("textEdit_2");
if (textEditPtr_DEBUG) {
} else {
}
textEditPtr_DEBUG->append(text);
Keep Calm and Carry On
|
|
|
|
|
Well, it's pretty obvious this call:
QTextEdit *textEditPtr_DEBUG = MWCCF->centralWidget()->findChild<QTextEdit *>("textEdit_2");
didn't find and return the object you thought it did.
Whatever "textEdit_2" is either doesn't exist or it's not in the container you think it is and are telling the code it's in.
I know you're saying "OK here", but is it really? Put a breakpoint on the if statement right above that and run the code under the debugger. When it breaks, hover the mouse over textEditptr_DEBUG and see if it actually has a value that isn't a bunch of zeros and points to the actual object you think it does.
|
|
|
|
|
Message Closed
modified 26-Oct-23 12:36pm.
|
|
|
|
|
Hell if I know. I haven't C++'d in about 15 years and I don't do Qt.
The process of checking return values for exactly what you expect them to be is just universal.
|
|
|
|
|
The code, and the problem, is quite clear.
1 QTextEdit *textEditPtr_DEBUG = MWCCF->centralWidget()->findChild<QTextEdit *>("textEdit_2");
2 if (textEditPtr_DEBUG)
3 {
4 text = " Found textEdit "; OK here
5 qDebug() << text;
6 textEditPtr_DEBUG->append(text);
7
8 text = " DEBUG START DEBUG trace with textEdit_2" ;
9 qDebug() << text;
10 textEditPtr_DEBUG->append(text);
11 }
12 else
13 {
14 text = "Did not find any QTextEdit";
15 qDebug() << text;
16 }
17 text = " DEBUG START DEBUG trace with textEdit_2" ;
19 textEditPtr_DEBUG->append(text); crash - null pointer here ??
At line 1 you (try to) create a pointer to some object.
At line 2 you test if that object is NULL, and if it is not, then you execute lines 4 to 10
If the object is NULL, then you skip to line 14
But then at line 19 you try to use a pointer which you may already have determined is NULL.
Unfortunately the debug output you have shown above does not match that code, so I am making some assumptions here.
[edit]
Also it does not help you to have the same debug message in two different places.
[/edit]
modified 25-Oct-23 5:13am.
|
|
|
|
|
Pretty good assumptions, I believe.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Richard MacCutchan wrote: If the object is NULL, then you skip to line 14
The OP shows the posted output. For example the following line.
" Sucess Found textEdit "
So line 5 is being executed (and the other lines in that block by the rest of the output.)
Of course the code is still wrong if the null pointer does show up.
Richard MacCutchan wrote: the debug output you have shown above does not match that code
Perhaps that is the actual problem. They think they are running the code shown but the code being executed is different. I have had that happen to me. Often enough that I probably do compiles, cleans and builds far more often than is needed.
|
|
|
|
|
jschell wrote: Perhaps that is the actual problem. They think they are running the code shown but the code being executed is different. Given who I suspect is asking the question I am not at all surprised.
And if you look at OP's reply to Dave Kreskowiak you will see more confusion.
|
|
|
|
|
I am trying to extract MAC address from text, and it is failing.
Here is the debug print of the "inString" text to extract the MAC from :
"Inquiring ...\n\t98:D3:31:F8:39:33\tclock offset: 0x7c6b\tclass: 0x001f00\n"
Here is my temporary code with regular expression which must be in error, but I do not see it.
QRegularExpression re(" [^\x00-\x7F]+\\ *(?:[^\x00-\x7F]| )*");
QRegularExpressionMatch match = re.match(inString);
if (match.hasMatch()) {
text = " Has match ";
QStringList result = match.capturedTexts();
text += result.at(0);
}
else
{
text = " NO match found ";
}
qDebug() << text;
Here is "run time error " , I am not sure why " invalid object" .
Does it mean my regular expression is incorrect ?
QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object
" NO match found "
If additional info would help, please ask.
Help analyzing the expression would be also greatly appreciated.
Thanks
|
|
|
|
|
You can validate your regex at any number of sites that do that sort of thing. That would help you determine if the problem is the regex itself, or the QRegularExpression object.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I can see one thing wrong with the expression right off the bat.
You're trying to match hexadecimal characters from 00 to FF, but the range you specify is only 00 to 7F.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Yes, I found that error too. It still fails. I am going t0 verify the expression using on one of the test sites...
|
|
|
|
|
Mrs Google found this
Regex.ai - Artificial Intelligence Regular Expression Generator[^]
Now for questions :
It is unclear how to select multiple entries from the text AKA highlight singe entry works ,
now to to select another part - dedicated by bold ?
Bold text
Devices:\n\thci0\t00:15:83:15:A2:CB\n
For old fart the text is too small - is there a way to make it bigger or do I have to tell Ubuntu ?
|
|
|
|
|
Another option would be something like:
int pos1 = index of first \t in inString;
int pos2 = index of second \t in inString;
int length = pos2 - pos1;
string MAC = inString.substring(pos1, length);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Yes, this just proves there is more than one way to skin a cat...
I did checked two AI regular expression generators and they came up with two solutions..
One pretty goofy.
My current preference to match MAC address is :
([0-F]{2}[:]){5}[0-F]{2})
it "finds" the address, but also finds a single two character in it.
The interesting part , the [0-F] apparently searches for characters between ASCII O and F - it does not search for digits. ( My opinion)
|
|
|
|
|
The range [0-F] includes the characters :;<=>@ , and does not include lowercase letetrs. You're better off either with [0-9a-fA-F] . Alternatively, if your regex engine has character classes then you could use ([[:xdigit:]]{2}[:]){5}[[:xdigit:]]{2} I'm not sure you need to specify the colon as a match set, unless regex treats them as special characters.
Keep Calm and Carry On
|
|
|
|
|
Thanks, as long as the entry is characters , not numbers, it makes sense.
Since the MAC address "hex numbers" is capitals I do not need [a-f ].
You have a good point - I'll try to delete [:] - in theory it should work.
|
|
|
|
|
Salvatore Terress wrote: Thanks, as long as the entry is characters , not numbers, it makes sense.
Why would they not be characters? You seem to be reading input from another command, so it should always be characters.
Salvatore Terress wrote: Since the MAC address "hex numbers" is capitals I do not need [a-f ].
That depends. Can you guarantee that you'll always get upper case hex digits? Under linux, the arp commands uses lower case letters for the MAC address. Others might as well
Salvatore Terress wrote: You have a good point - I'll try to delete [:] - in theory it should work.
It should. But as noted above, it would depend on where you're getting your input from. Under Windows, if you run ipconfig /all you get the Mac address separated by dashes e.g. "00-4B-35-30-35-34", so you may need to allow for that too.
Keep Calm and Carry On
|
|
|
|
|
Salvatore Terress wrote: ([0-F]{2}[:]){5}[0-F]{2}) Should be:
([0-F]{2}[:]){5}[0-F]{2}
modified 25-Oct-23 2:01am.
|
|
|
|
|
Google seems to return regular expressions to validate a mac address. Not to mention explaining forms that might be less than obvious.
mac address regular expression
Other than that the square brackets are used for character matching. Single character. Best I can tell you seem to be trying to match a sequence of characters (2 digit hex.)
|
|
|
|