|
Many thanks for all the support.
FYI
I did use "[ -~]+" to extract first continuous words. Now I am working on to extract ALL words...
I did try pass "[/\w+/g]+" to my otherwise working function and ended up with [/w+/g]+
- the back slash is missing,
And this is result , part of my debug messages
" instring text \t\n Waiting to connect to bluetoothd..."
" regular expression \t\n [/w+/g]+"
" Has all match g"
You asked for the string I am trying to extract stuff from
here it is
"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# "
As you can see - the extracted "g" is from "Agent". No good...
I suspect Qt is messing with passing the backslash...
result = BTUL->EditLine_RegExp_Ext(result, "[/\w+/g]+",textEditPtr_DEBUG, textEditPtr_DEBUG);
<pre lang="C++">
"Waiting to connect to bluetoothd..."
"Waiting to connect to bluetoothd..."
"START EditLine_RegExp...QString BT_Utility_Library::EditLine_RegExp_Ext(QString, QString, QTextEdit *, QTextEdit *)1321"
" instring text \t\n Waiting to connect to bluetoothd..."
" regular expression \t\n [/w+/g]+"
" Has all match g"
|
|
|
|
|
The backslash character is used as the escape character within strings, so you need to escape it:
"[/\\w+/g]+"
|
|
|
|
|
Salvatore Terress wrote: Where is my error ??
Your question is not specific to regular expressions but also to what is running the regular expression engine. But you did not provide that information.
A 'g' is something that is external to regular expressions. So where you are using it is important and the only clue you provided is 'EditLine_RegExp' which googling for returned no results. But certainly since you didn't escape the backslash for the g that would never work.
Other than that of course there is also the following
- A space is not considered a word. Your capture group includes that.
- There could be more than one space.
- Are you matching on a single line? If not there are other complications.
- If there is ONLY words in your line then it is pointless to use regex at all. Just split it.
- If there are OTHER things besides words then I don't believe what you are doing will work (but again you didn't state what so maybe it is.)
|
|
|
|
|
Your question is not specific to regular expressions but also to what is running the regular expression engine. But you did not provide that information.
Since most of "AI reg expressions" generators are working and Qt SAME expression does not - you have a point.
I guess I will ask in Qt forum about that.
Yes, there are other means to verify that the string contains desired word, (QString "contains" method works peachy )
however, I sure like to learn more about using regular expression - so I like to stick with reg expressions for now.
ADDENDUM
My post is about using regular expression - it is NOT about the function I am using to actually implement regular expression. That function works as expected and there is no need to evacuate that function here.
If it did not work as desired I would say so.
|
|
|
|
|
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
|
|
|
|