^([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(([-+]?)([\d]{1,3})((\.)(\d+))?)$
I am trying to use this regex above to be able to confirm that the data is a valid coordinate. I am having trouble getting this to work with Firebase rules. When i run the regex in an online regex tester, it works okay, but Firebase rules doesn't seem to accept it.
Here is my firebase rule:
".validate": "newData.isString() && newData.val().matches(/^([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(([-+]?)([\d]{1,3})((\.)(\d+))?)$/)"
Is there anyway to get this working?
You need to double the escaping backslashes, but honestly, your expression contains too many redundant grouping constructs.
Use
.matches(/^[-+]?\\d{1,2}\\.\\d+,[-+]?\\d{1,3}(\\.\\d+)?$/)
or avoid the backslashes altogether:
.matches(/^[-+]?[0-9]{1,2}[.][0-9]+,[-+]?[0-9]{1,3}([.][0-9]+)?$/)
The regex will match strings like in this online demo.
Details:
^ - start of string (in Firebase regex, it is an anchor when used at the start of the pattern only)
[-+]? - 1 or 0 + or -
[0-9]{1,2} - 1 or 2 digits
[.] - a dot
[0-9]+ - 1+ digits
, - a comma
[-+]? - 1 or 0 + or -
[0-9]{1,3} - 1 to 3 digits
([.][0-9]+)? - 1 or 0 sequences of . and 1+ digits (note that non-capturing groups are not supported)
$ - end of string anchor (only when at the pattern end, $ matches the end of string in Firebase regex).
Related
I'm looking for a regular expression tester for Google Big Data (RE2) reg expressions. There are a few testers out there, but none of them seems to understand my statement. These are the ones I've tried and they've worked for simple expressions but not with mine:
https://regex101.com/
https://www.regextester.com
https://www.analyticsmarket.com/freetools/regex-tester/
This is my regex:
^(?:1-)?((?:R|RO|Ro)?[:|.]?\\s?\\d{3}[-|.]?\\d{4}[-|/]F\\d{2}-\\d{2})$
where I would process strings like these:
Ro 708-2859/F07-01
RO708-2859-F06-04
RO703-3877-F01
1-RO520-0628-F08
RO6868847-000-010
Does anyone have an idea of how I might enter the statement different or where I could test it?
You can use
^(?:1-)?((?:R[Oo]?)?[:.]?\s?\d{3}[-.]?\d{4}[-/](?:F\d{2}(?:-\d{2})?|\d{3}[-/]\d{3}))$
See the regex demo. Details:
^ - start of string
(?:1-)? - an optional 1- string
((?:R[Oo]?)?[:.]?\s?\d{3}[-.]?\d{4}[-/](?:F\d{2}(?:-\d{2})?|\d{3}[-/]\d{3})) - Group 1:
(?:R[Oo]?)? - an optional sequence of R and then an optional O or o
[:.]? - an optional : or .
\s? - an optional whitespace
\d{3} - three digits
[-.]? - an optional - or .
\d{4} - four digits
[-/] - - or /
(?:F\d{2}(?:-\d{2})?|\d{3}[-/]\d{3}) - either F, two digits and then an optional sequence of - and two digits, or three digits, - or / and three digits
$ - end of string.
See the Google Sheets demo:
You may try to use https://www.regexplanet.com/advanced/golang/index.html
I've tried your regexp, and this also pointing to Re2 for docs.
Background:
I need to identify a pair of numbers separated by a hyphen (-), the numbers can optionally include +/- and can be decimal.
So below are examples of that:
3-4, +3-+4, .3-.4, 0.3-0.4, -0.3--0.4, 0.3--0.4 etc...
I was using below expression:
(-?\+?\d*.?\d*)-(-?\+?\d*.?\d*)
It works well in most cases but fails in below:
-0.3--0.4
The groups it forms are: -0.3- and 0.4
But if i replace it like:
(-?\+?\d*.?\d+)-(-?\+?\d*.?\d+), it works fine.
I am wondering what difference replacing the * with + is making?
We have used this in javascript.
The wrong capturing is accounted for by the fact that your patterns inside capturing groups (-?\+?\d*.?\d*) can match an empty string and - more importantly here - . matches any char, not only a dot. You must escape it to match a literal dot. Note how (-?\+?\d*.?\d*)-(-?\+?\d*.?\d*) matches 3-4, (the , is captured with Group 2 pattern .) and note Matches 5 and 6 where . matches a space and a hyphen.
Also, your -?\+? actually allows matching -+ sequence of signs, which does not seem what you need. Just use [-+]? optional character class.
So, you might want to use ([-+]?\d*\.?\d*)-([-+]?\d*\.?\d*) pattern, but I'd advise to make sure at least 1 digit is matched, and you may use ([-+]?\d*\.?\d+)-([-+]?\d*\.?\d+) pattern for it.
Details:
([-+]?\d*\.?\d+) - Group 1: a sequence of
[-+]? - an optional - or +
\d* - 0+ digits
\.? - an optional .
\d+/\d* - 1 or more digits (or 0 or more with *)
- - a hyphen
([-+]?\d*\.?\d+) - see above.
I'm having issues with the validation of the chilean RUT/RUN with a regex expression in PCRE. I have the next regular expression but sadly can't make it work:
\b[0-9|.]{1,10}\-[K|k|0-9]
I need help to see what is wrong with the code. The application I need to use only uses PCRE.
Thank you.
You may use
^(\d{1,3}(?:\.\d{1,3}){2}-[\dkK])$
to match and capture (that is not usually necessary, but your app requires a capturing group to extract its contents) a whole string that matches the pattern. See the regex demo.
To match shorter strings that match this pattern inside a larger string, you may remove ^ and $ (see demo) or use \b word boundaries instead (see this demo).
Details:
^ - start of string
\d{1,3} - 1 to 3 digits
(?:\.\d{1,3}){2} - 2 sequences of a literal . and 1 to 3 digits
- - a hyphen
[\dkK] - a digit, k or K.
$ - end of string.
As they sometimes omit the dots, I used this one:
^(\d{1,2}(?:[\.]?\d{3}){2}-[\dkK])$
Details:
^ - start of string
\d{1,2} - 1 or 2 digits
(?:[.]?\d{3}){2} - 2 sequences of an optional '.' and 3 digits
- a hyphen
[\dkK] - a digit, k or K
$ - end of string
1234567-k OK
12345678-k OK
1.234.567-k OK
12.345.678-k OK
known issue:
12.345678-k and 12345.678-k still OK and I do not like this :(
You need to change to ^(\d{1,3}(?:\.\d{3}){2}-[\dkK])$ to capture only 2 sequence of 3 digits after the first sequence of 1-3 digits.
please consider being more specific in the REGEX build, since it matched wrong numbers, such as 17.87.335-2. Also the included one did't match formats without the dots or the hyphens.
Please consider using the following format: \b(\d{1,3}(?:(.?)\d{3}){2}(-?)[\dkK])\b
Modified prior version to try the other formats: https://regex101.com/r/2Us0j6/9
^([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(([-+]?)([\d]{1,3})((\.)(\d+))?)$
I am trying to use this regex above to be able to confirm that the data is a valid coordinate. I am having trouble getting this to work with Firebase rules. When i run the regex in an online regex tester, it works okay, but Firebase rules doesn't seem to accept it.
Here is my firebase rule:
".validate": "newData.isString() && newData.val().matches(/^([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(([-+]?)([\d]{1,3})((\.)(\d+))?)$/)"
Is there anyway to get this working?
You need to double the escaping backslashes, but honestly, your expression contains too many redundant grouping constructs.
Use
.matches(/^[-+]?\\d{1,2}\\.\\d+,[-+]?\\d{1,3}(\\.\\d+)?$/)
or avoid the backslashes altogether:
.matches(/^[-+]?[0-9]{1,2}[.][0-9]+,[-+]?[0-9]{1,3}([.][0-9]+)?$/)
The regex will match strings like in this online demo.
Details:
^ - start of string (in Firebase regex, it is an anchor when used at the start of the pattern only)
[-+]? - 1 or 0 + or -
[0-9]{1,2} - 1 or 2 digits
[.] - a dot
[0-9]+ - 1+ digits
, - a comma
[-+]? - 1 or 0 + or -
[0-9]{1,3} - 1 to 3 digits
([.][0-9]+)? - 1 or 0 sequences of . and 1+ digits (note that non-capturing groups are not supported)
$ - end of string anchor (only when at the pattern end, $ matches the end of string in Firebase regex).
I currently have this regex:
/^\+\(?([0-9]{1,4})\)?$/g
Now, I can use the following formats:
+31
+(31)
+(31
+31)
However, I want to modify it so only with both parentheses it'll work, not just with one. So +(31 and +31) won't work anymore. How can I manage to get it?
Create an alternation group to match digits inside parentheses or with no parentheses:
^\+(?:\([0-9]{1,4}\)|[0-9]{1,4})$
See regex demo
Details:
^ - start of string anchor
\+ - a literal +
(?:\([0-9]{1,4}\)|[0-9]{1,4}) - one of the two alternatives:
\([0-9]{1,4}\) - 1 to 4 digits enclosed with literal ( and )
| - or
[0-9]{1,4} - 1 to 4 digits
$ - end of string anchor