Regular Expression in Qt - c++

I am using Qt5.6.
I need to process the incoming data in a serial port, the data will be of the format "AD=+172345AD=+272345" and so on. I append the incoming data to a QString and using Regex to extract the decimals.
If I write a regular expression :
int tmp = StrData.indexOf(QRegularExpression("AD=\+[0-9]{6}"))
it doesn't match, i.e tmp is always -1. But I tested the regex here, and I found it to be valid. What could be the issue?

As per the docs, you can use the regex inside QString.indexOf to get the index position of the first match of the regular expression re in the string.
The only problem with the regex is that in Qt, the strings are C style, i.e. they can contain escape sequences. Thus, the backslashes escaping regex special characters must be doubled.
Use
QRegularExpression("AD=\\+[0-9]{6}")
or
QRegularExpression("AD=[+][0-9]{6}")
since inside [...], the + is treated as a literal character.

Related

How to exclude the last character from the below reg expression response

I am having the below text response I created a regular expression but I want to exclude the last character from the response.
INPUT:
href=\"admin_task_detail?rid=d652a3dd-fff0-4174-a065-5158298493af&tid=4947358c-9f5a-4174-8699-7fa12f9ac3c8&v=5ecb5743a92e7\" >T4947-C3C8<\/a>","#data-order":"T4947-C3C8"},
Reg Exp which I tried
tid=(.+?)\"
Results:
Match[1][0]=tid=4947358c-9f5a-4174-8699-7fa12f9ac3c8&v=5ecb5743a92e7\"
Match[1][1]=4947358c-9f5a-4174-8699-7fa12f9ac3c8&v=5ecb5743a92e7\
The slash symbol need to get excluded from the response.
Just escape the backslash in the regex:
tid=(.+?)\\"
Demo & explanation
Are you absolutely sure that you need this:
tid=4947358c-9f5a-4174-8699-7fa12f9ac3c8&v=5ecb5743a92e7
and not this:
tid=4947358c-9f5a-4174-8699-7fa12f9ac3c8
?
Because these tid and v are different parameters of the query string and most probably you need only tid, not v.
In first case the relevant regular expression would be tid=(.+?)\\
In the second case you will need tid=(.+?)&
The tricky point is that backslash is a meta character so it needs to be escaped by the extra backslash.
In the majority of cases (like in your one) it's easier and better to use Boundary Extractor where you don't need to deal with these regular expressions, it's sufficient to provide left and right boundaries in the plain text. Check out The Boundary Extractor vs. the Regular Expression Extractor in JMeter article for more details.

Regular expression formatting issue

I'm VERY new to using regular expressions, and I'm trying to figure something simple out.
I have a simple string, and i'm trying to pull out the 590111 and place it into another string.
HMax_590111-1_v8980.bin
So the new string would simply be...
590111
The part number will ALWAYS have 6 digits, and ALWAYS have a version and such. The part number might change location inside of the string.. so it needs to be able to work if it's like this..
590111-1_v8980_HMXAX.bin
What regex expression will do this? Currently, i'm using ^[0-9]* to find it if it's in the front of the file.
Try the following Regex:
Dim text As String = "590111-1_v8980_HMXAX.bin"
Dim pattern As String = "\d{6}"
'Instantiate the regular expression object.
Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)
'Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
In Regex \d denotes numerics, so first you write \d.
Then as you know there will be a fix length of numbers which can be specified in Regex with "{}". If you specify \d{6} it means it will expect 6 continuous occurrences of a numeric character.
I would recommend to use this site to try your own expressions. Here you can also find a little bit of information about the expressions you are building if you hover over it.
Regex Tester

Parsing variables within a string using a Regular Expression

I've got a bit of a problem with regular expressions with ColdFusion.
I have a string:
Hi my name is {firstname}. and i live in {towncity} my email address is {email}
What I would like to know is how would I go about finding all strings, within my string, that are encased within a set of {} brackets? I would like to split all the matching strings into an array so I can use the results of query data.
Also is this a commonly used pattern for processing strings within matching strings for merging variable data ?
Any help greatly appreciated.
Simple Answer
To find all the brace-encased strings, you can use rematch and the simple expression \{[^{}]+\}
Explanation
The backslashes \ before each brace are to escape them, and have them act as literal braces (they carry special meaning otherwise).
The [^...] is a negative character class, saying match any single char that is NOT one of those contained within, and the greedy + quantifier tells it to match as many as possible, but at least one, from the preceding item.
Thus using [^{}]+ between the braces means it will not match nested or unmatched braces. (Whilst using \{.*?\} could match two opening braces. Note: the *? is a lazy quantifier, it matches nothing (if possible), but as many as required.)
Extended Answer
However, since you say that the results come from a query, a way to only match the values you're dealing with is to use the query's ColumnList to form an expression:
`\{(#ListChangeDelims(QueryName.ColumnList,'|')#)\}`
This changes ColumnList into a pipe-delimited list - a set of alternatives, grouped by the parentheses - i.e. the generated pattern will be like:
\{(first_name|towncity|email)\}
(with the contents of that group going into capture group 1).
To actually populate the text (rather than just matching) you could do something similar, except there is no need for a regex here, just a straight replace whilst looping through columns:
<cfloop index="CurColumn" list=#QueryName.ColumnList#>
<cfset text = replace( text , '{#CurColumn#}' , QueryName[CurColumn][CurrentRow] , 'all' ) />
</cfloop>
(Since this is a standard replace, there's no need to escape the braces with backslashes; they have no special meaning here.)
Use the reMatch(reg_expression, string_to_search) function.
The details on Regular Expressions in Coldfusion 10 are here. (I believe the regexp in CF8 would be roughly the same.)
Use the following code.
<cfset str = "Hi my name is {firstname}. And I live in {towncity} my email address is {email}.">
<cfoutput>Search string: <b>#str#</b><br />Search result:<br /></cfoutput>
<cfset ret = reMatch("\{[\w\s\(\)\+\.#-]+\}", str)>
<cfdump var ="#ret#">
This returns an array with the following entries.
{firstname}
{towncity}
{email}
The [] brackets in CF regular expressions define a character set to match a single character. You put + after the brackets to match one or more characters from the character set defined inside the []. For example, to match one or more upper case letters you could write [A-Z]+.
As detailed in the link above, CF defines shortcuts to match various characters. The ones I used in the code are: \w to match an alpha-numeric character or an underscore, \s to match a whitespace character (including space, tab, newline, etc.).
To match the following special characters +*?.[^$({|\ you escape them by writing backslash \ before them.
An exception to this is the dash - character, which cannot be escaped with a backslash. So, to use it as a literal simply place it at the very end of the character set, like I did above.
Using the above regular expression you can extract characters from the following string, for example.
<cfset str = "Hi my name is { John Galt}. And I live in {St. Peters-burg } my email address is {john#exam_ple.com}.">
The result would be an array with the following entries.
{ John Galt}
{St. Peters-burg }
{john#exam_ple.com}
There may be much better ways to do this, but using something like rematch( '{.*?}', yourstring ) would give you an array of all the matches.
For future reference, I did this with the excellent RegExr, a really nice online regex checker. Full disclosure, it's not specifically for ColdFusion, but it's a great way to test things out.

Regular Expression Time Format?

I have a regular expression which accept time in a specific format like the following,
"10:00".
I want to change the regular expression to etiher accpet this format or accept only a single one dash only ("-").
Here is the expression:
/^((\d)|(0\d)|(1\d)|(2[0-3]))\:((\d)|([0-5]\d))$/
Key points to solving this:
Square brackets ([ and ]) are used to enclose character classes.
The pipe | means or.
[\:|-] means chech for either a literal : or a hyphen -.
The resulting pattern is:
^((\d)|(0\d)|(1\d)|(2[0-3]))[\:|-]((\d)|([0-5]\d))$
Just use an alternative:
^<your regex>$|^-$
This will match either a time in your format or a single hyphen-minus.
Does this regex need to have so many brackets?
/^(([01]?\d|2[0-3]):[0-5]\d|-)$/

Regular expression for XML

I'm attempting to build a regular expression that will match against the contents of an XML element containing some un-encoded data. Eg:
<myElement><![CDATA[<p>The <a href="http://blah"> draft </p>]]></myElement>
Usually in this circumstance I'd use
[^<]*
to match everything up to the less than sign but this isn't working in this case. I've also tried this unsuccessfully:
[^(</myElement>)]*
I'm using Groovy, i.e. Java.
Please don't do this, but you're probably looking for:
<myElement>(.*?)</myElement>
This won't work if <myElement> (or the closing tag) can appear in the CDATA. It won't work if the XML is malformed. It also won't work with nested <myElement>s. And the list goes on...
The proper solution is to use a real XML parser.
Your [^(</myElement>)]* regex was saying: match any number of characters that are not in the set (, <, /, m, etc., which is clearly not what you intended. You cannot place a group within a character class in order for it to be treated atomically -- the characters will always be treated as a set (with ( and ) being literal characters, too).
if you are doing it on a line by line basis, this will match the inside if your example:
>(.*)</
returns: <![CDATA[<p>The <a href="http://blah"> draft </p>]]>
Probably use it something like this:
subjectString = '<myElement><![CDATA[<p>The <a href="http://blah"> draft </p>]]></myElement>';
Matcher regexMatcher = subjectString =~ ">(.*)</"
if (regexMatcher.find()) {
String ResultString = regexMatcher.group();
}