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
Related
The following combinations should be covered by this regex:
test-ds-s**
test-s**
test-d**
(** two numbers from 0-9)
My regex looks like this: ^test-(ds-)?[ds]\\d{2,2}$
But now test-ds-d** is also possible, what I dont want. Is there any way to make the d only possible, when the optional ds- part is not used?
You can use
^test-(ds-(?!d))?[ds]\d{2}$
See the regex demo.
Details
^ - start of string
test- - a fixed string
(ds-(?!d))? - an optional capturing group matching ds- if not immediately followed with d
[ds] - d or s
\d{2} - two digits
$ - 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).
^([-+]?)([\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 need to match these values:
(First approach to a regex that roughly does what I want)
\d+([.,]\d{3})*[.,]\d{2}
like
24,56
24.56
1.234,56
1,234.56
1234,56
1234.56
but I need to not match
1.234.56
1,234,56
So somehow I need to check the last occurrence of "." or "," to not be the same as the previous "." or ",".
Background: Amounts shall be matched in English and German format with (optional) 1000-Separators.
But even with help of regex101 I completely fail at coming up with a correctly working look-behind. Any suggestions are highly appreciated.
UPDATE
Based on the answers I got so far, I came up with this (demo):
\d{1,3}(?:([\.,'])?\d{3})*(?!\1)[\.,\s]\d{2}
But it matches for example 1234.567,23 which is not desirable.
You may capture the digit grouping symbol and use a negative lookahead with a backreference to restrict the decimal separator:
^(?:\d+|\d{1,3}(?:([.,])\d{3})*)(?!\1)[.,]\d{2}$
^ ^ ^^^^^
See the regex demo
Group 1 will contain the last value of the digit grouping symbol and (?!\1)[.,] will match the other symbol.
Details:
^ - start of string
(?:\d+|\d{1,3}(?:([.,])\d{3})*) - either of the two alternatives:
\d+ - 1+ digits
| - or
\d{1,3} - 1 to 3 digits,
(?:([.,])\d{3})* - zero or more sequences of:
([.,]) - Group 1 capturing . or ,
\d{3} - 3 digits
(?!\1)[.,] - a . or , but not equal to what was last captured with ([.,]) pattern above
\d{2} - 2 digits
$ - end of string.
You can use
^\d+(([.,])\d{3})*(?!\2)[.,]\d{2}$
live demo
I want to match expression like (010)12345678 or 010-12345678. Now my regular expression is \(?\d{3}[)-]\d{8}, but it where match expression like (010-12345678. How should I write a true regular expression?
EDIT:
I write a right regex like (\(\d{3}\)|[^(]\d{3}-)\d{8} that can meet my needs.
Note that (\(\d{3}\)|[^(]\d{3}-)\d{8} will also match a non-( before a number that starts with 3 digits and - due to [^(].
You need to remove it and use
(\(\d{3}\)|\d{3}-)\d{8}
^^^^^^^^^^^^^^^^^^
See demo. Or an anchored version:
^(\(\d{3}\)|\d{3}-)\d{8}$
See another demo
Pattern details:
^ - start of string
(\(\d{3}\)|\d{3}-) - Group 1 matching either of the 2 alternatives:
\(\d{3}\) - ( followed with 3 digits and a literal )
| - or
\d{3}- - 3 digits and a literal -
\d{8} - 8 digits
$ - end of string.
Depending on the regex engine, \d might need replacing with [0-9].