Invalid regular expression:Lone quantifier brackets - regex

I have a html phone pattern that will accept these formats :
+61 x xxxx xxxx,
+61xxxxxxxxx,
0x xxxx xxxx,
0xxxxxxxxx,
xxxx xxxx,
xxxxxxxx,
+xx xxx xxx xxx,
+xxxxxxxxxxx,
0xxx xxx xxx,
0xxxxxxxxx
It was working few months ago, now suddenly my phone fields are not validating . I'm having this error:
Pattern attribute value ^(?:0|\(?\+61\)?\s?|0061\s?)[1-79](?:[\.\-\s]?\d\d){4}|(\d{4}[\s]\d{4})|(\d{8})|(\d{4}[\s]\d{3}[\s]\d{3})|(\+61\[\s]\d{3}[\s]\d{3}[\s]\d{3})|(\+61\s\d{3}\s\d{3}\s\d{3})$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^(?:0|\(?\+61\)?\s?|0061\s?)[1-79](?:[\.\-\s]?\d\d){4}|(\d{4}[\s]\d{4})|(\d{8})|(\d{4}[\s]\d{3}[\s]\d{3})|(\+61\[\s]\d{3}[\s]\d{3}[\s]\d{3})|(\+61\s\d{3}\s\d{3}\s\d{3})$/: Lone quantifier brackets

So far, no one cared to show where in your pattern the error is.
…|(\+61\[\s]\d{3}[\s]\d{3}[\s]\d{3})|(\+61\s\d{3}\s\d{3}\s\d{3})$
^
There by mistake you inserted a backslash, escaping the opening bracket, so making it an ordinary character and leaving the closing bracket Lone. (Sadly the error message is somewhat misleading, since those brackets are of course not quantifier brackets.)

Similar to the request above, I received the error in Visual Studio Code using the following RegEx to capture words wrapped in brackets:
\[([^\\[\r\n]*(?:\\.[^\\]\r\n]*)*)\] (which is incorrect)
on the text string (SQL)
SELECT d.[fname]
,d.[lname],d.[address1],d.[address2],d.[City],d.[zip]
FROM my_table d
WHERE [hh_id] IN (SELECT [HhId]
FROM other_table)
However using this RegEx pattern in online regex tools shown in https://regex101.com/r/HGycR9/1 show it is valid RegEx.
The correct RegEx pattern is \[([^[\r\n]*(?:\\.[^\r\n]*)*)\], which will highlight:
SELECT d.[fname] ,d.[lname],d.[address1],d.[address2],d.[City],d.[zip]
FROM my_table d WHERE [hh_id] IN (SELECT [HhId] FROM other_table)
Then you can use $1 in the replace field of VS Code to remove the brackets and leave the words contained within them.
SELECT d.fname
,d.lname,d.address1,d.address2,d.City,d.zip
FROM my_table d
WHERE hh_id IN (SELECT HhId
FROM other_table)

Just to spell it out a bit more explicitly, the difference between VSCode (and apparently Visual Studio) and other Java/ECMAScript regex interpreters is that the VS products are picky about matching brackets, even if they should be interpreted as literal characters.
For example, the regex below - with just the first bracket escaped - is fine with matching a string surrounded by square brackets in most JS-based (and PCRE) regex interpreters. e.g.: [string]
\[\w+]
^
While just the "opening" square bracket is escaped, the "closing" square bracket is also interpreted as a literal character. Semantically, with the first bracket escaped, you haven't actually started a character class match (or group or quantifier, if it's parentheses () or curly braces {} you're matching).
In VSCode, you must also escape the "closing" bracket, or else you get the "Lone quantifier brackets" error.
\[\w+\]
^ ^
Another irritating aspect is that the .NET regex interpreter behaves very similarly to a standard Javascript interpreter, so it similarly does not care about escaping both brackets if the opening one is escaped. Something of this nature that's fine in .NET or Powershell won't work the same in VSCode.

That means exactly that, pattern invalid.
If you want to match phones from Australia, you could use:
pattern="^(?:0|\(?\+61\)?\s?|0061\s?)[1-79](?:[\.\-\s]?\d\d){4}$"
Pattern found here.
Example:
https://jsfiddle.net

Related

What do pipes between brackets mean in a regex?

Reading this vim plugin I see this line:
syntax match tweeDelimiter "[<<|>>|\]\]|\[\[]"
To me, that regex doesn't make much sense when it's surrounded by []. According to this, "POSIX bracket expressions match one character out of a set of characters".
So isn't this matching < or > or [ or ]? I know from context that it's trying to match << or >> or [[ or ]].
That indeed looks like a bug in the plugin. If it wants to match pairs of those characters, it has to use plain regexp branches (\|), not a collection:
<<\|>>\|\]\]\|\[\[
If there were additional stuff to match, above would have to be enclosed in \%(...\) to group it. However, using [...] will match any of the contained characters; Vim just ignores the duplicate ones. As others have commented already, such could be written in shorter form, for example [][<>|].
So, if the plugin indeed mistakenly matches stuff like <> and <[ instead of just << and [[, please inform its author about the bug.

How to do regular Expression in AutoIt Script

In Autoit script Iam unable to do Regular expression for the below string Here the numbers will get changed always.
Actual String = _WinWaitActivate("RX_IST2_AM [PID:942564 NPID:10991 SID:498702881] sbivvrwm060.dev.ib.tor.Test.com:30000","")
Here the PID, NPID & SID : will be changing and rest of the things are always constant.
What i have tried below is
_WinWaitActivate("RX_IST2_AM [PID:'([0-9]{1,6})' NPID:'([0-9]{1,5})' SID:'([0-9]{1,9})' sbivvrwm060.dev.ib.tor.Test.com:30000","")
Can someone please help me
As stated in the documentation, you should write the prefix REGEXPTITLE: and surround everything with square brackets, but "escape" all including ones as the dots (.) and spaces () with a backslash (\) and instead of [0-9] you might use \d like "[REGEXPTITLE:RX_IST2_AM\ \[PID:(\d{1,6})\ NPID:(\d{1,5})\ SID:(\d{1,9})\] sbivvrwm060\.dev\.ib\.tor\.Test\.com:30000]" as your parameter for the Win...(...)-Functions.
You can even omit the round brackets ((...)) but keep their content if you don't want to capture the content to process it further like with StringRegExp(...) or StringRegExpReplace(...) - using the _WinWaitActivete(...)-Function it won't make sense anyways as it is only matching and not replacing or returning anything from your regular expression.
According to regex101 both work, with the round brackets and without - you should always use a tool like this site to confirm that your expression is actually working for your input string.
Not familiar with autoit, but remember that regex has to completely match your string to capture results. For example, (goat)s will NOT capture the word goat if your string is goat or goater.
You have forgotten to add a ] in your regex, so your pattern doesn't match the string and capture groups will not be extracted. Also I'm not completely sold on the usage of '. Based on this page, you can do something like StringRegExp(yourstring, 'RX_IST2_AM [PID:([0-9]{1,6}) NPID:([0-9]{1,5}) SID:([0-9]{1,9})]', $STR_REGEXPARRAYGLOBALMATCH) and $1, $2 and $3 would be your results respectively. But maybe your approach works too.

Regex working in regex engine but not in postgresql

I tried to match number 13 in pipe separated string like the one below:
13 - match
1|2|13 - match
13|1|2 - match
1|13|2 - match
1345|1|2 - should fail
1|1345|2 - should fail
1|2|1345 - should fail
1|4513|2 - should fail
4513|1|2 - should fail
2|3|4|4513- should fail
So, if 13 only occurs at the beginning or end, or in-between the string as a whole word it should match.
For that I wrote the following regex:
^13$|(\|13\|)?(?(1)|(^13\||\|13$))
In Regex101 it is working as expected. Please click link to see my sample.
But in Postgresql it throws error for the following query:
SELECT * FROM tbl_privilage WHERE user_id = 24 and show_id ~ '^13$|(\|13\|)?(?(1)|(^13\||\|13$))';
Error:
ERROR: invalid regular expression: quantifier operand invalid
SQL state: 2201B
Don't use a regex, using an array is more robust (and maybe more efficient as well):
select *
from the_table
where '13' = any (string_to_array(the_column, '|'));
this assumes that there is no whitespace between the values and the delimiter. You can even index that expression which probably makes searching a lot faster.
But I agree with Frank: you should really fix your data model.
Documentation is quite clear, saying that operator ~ implements the POSIX regular expressions. In Regex101 you're using PCRE (Perl-compatible) regular expressions. The two are very different.
If you need PCRE regular expressions in PostgreSQL you can setup an extension. Like pgpcre.
You need to match 13 within word boundaries.
You need
[[:<:]]13[[:>:]]
This solution should work even if you have spaces around the numeric values.
See documentation:
There are two special cases of bracket expressions: the bracket
expressions [[:<:]] and [[:>:]] are constraints, matching empty
strings at the beginning and end of a word respectively.

Trouble converting regex

This regex:
"REGION\\((.*?)\\)(.*?)END_REGION\\((.*?)\\)"
currently finds this info:
REGION(Test) my user typed this
END_REGION(Test)
I need it to instead find this info:
#region REGION my user typed this
#endregion END_REGION
I have tried:
"#region\\ (.*?)\\\n(.*?)#endregion\\ (.*?)\\\n"
It tells me that the pattern assignment has failed. Can someone please explain what I am doing wrong? I am new to Regex.
It seems the issue lies in the multiline \n. My recommendation is to use the modifier s to avoid multiline complexities like:
/#region\ \(.*?\)(.*?)\s#endregion\s\(.*?\)/s
Online Demo
s modifier "single line" makes the . to match all characters, including line breaks.
Try this:
#region(.*)?\n(.*)?#endregion(.*)?
This works for me when testing here: http://regexpal.com/
When using your original text and regex, the only thing that threw it off is that I did not have a new line at the end because your sample text didn't have one.
Constructing this regex doesn't fail using boost, even if you use the expanded modifier.
Your string to the compiler:
"#region\\ (.*?)\\\n(.*?)#endregion\\ (.*?)\\\n"
After parsed by compiler:
#region\ (.*?)\\n(.*?)#endregion\ (.*?)\\n
It looks like you have one too many escapes on the newline.
if you present the regex as expanded to boost, an un-escaped pound sign # is interpreted as a comment.
In that case, you need to escape the pound sign.
\#region\ (.*?)\\n(.*?)\#endregion\ (.*?)\\n
If you don't use the expanded modifier, then you don't need to escape the space characters.
Taking that tack, you can remove the escape on the space's, and fixing up the newline escapes, it looks like this raw (what gets passed to regex engine):
#region (.*?)\n(.*?)#endregion (.*?)\n
And like this as a source code string:
"#region (.*?)\\n(.*?)#endregion (.*?)\\n"
Your regular expression has an extra backslash when escaping the newline sequence \\\n, use \\s* instead. Also for the last capturing group you can use a greedy quantifier instead and remove the newline sequence.
#region\\ (.*?)\\s*(.*?)#endregion\\ (.*)
Compiled Demo

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.