^([-+]?)([\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
My goal is to validate instagram profile links via a regular expression.
So for example this one is valid:
https://www.instagram.com/test.profile/
This one is not:
https://www.instagram.com/explore/tags/test/
Using this regex
(?:(?:http|https):\/\/)?(?:www\.)?(?:instagram\.com|instagr\.am)\/([A-Za-z0-9-_\.]+)
On this text: https://www.instagram.com/explore/tags/test/
produces a match https://www.instagram.com/explore, but this one I want to avoid and discharge.
LIVE DEMO HERE
My question: is possible to add an additional syntax in the regex to validate a match ONLY if the string contains exactly 4 slashes (/)?
You can make the / char obligatory if you add \/ at the end:
^(?:https?:\/\/)?(?:www\.)?(?:instagram\.com|instagr\.am)\/([\w.-]+)\/$
Note that [a-zA-Z0-9_] can most probably be replaced with \w (especially, if it is JavaScript, PHP, Java or Ruby) to make the pattern shorter. It won't hurt even in those regex flavors where \w is Unicode-aware by default (Python re, .NET).
See the regex demo. Details:
^ - start of string
(?:https?:\/\/)? - an optional http:// or https://
(?:www\.)? - an optional www. string
(?:instagram\.com|instagr\.am) - instagram.com or instagr.am
\/ - a / char
([\w.-]+)- Group 1: one or more letters, digits, _, . or - chars
\/ - a / char
$ - end of string.
^([-+]?)([\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).
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
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