I came up with following regex for a mobile phone number:
^\+([0-9]{1,} )+([0-9]{2,} )+[0-9]+
Example of valid number:
+385 552 8221520
What would be the according regex, so I don't get a match if any whitespace in the third capturing group is found:
+385 552 82215 20 (gives a match now, but not fine!)
Your pattern contains quantified groupings. The ^\+([0-9]{1,} )+([0-9]{2,} )+[0-9]+ pattern matches a string that starts with +, then contains 1 or more repetitons of 1+ digits followed with a space, then 1+ repetitions of 2+ digits followed with a space and then 1+ digits. Thus, it matches many space separated digit chunks. Also, it is not anchored at the end of the string with $ and might match strings that contain rubbish at the end if used with a regex method that allows partial matches.
To limit to just three space separated digit chunks, you may use any of the following:
^\+[0-9]+ +[0-9]{2,} +[0-9]+$ - if there can be 1 or more spaces between the digit groups
^\+[0-9]+ ?[0-9]{2,} ?[0-9]+$ - if there can be 1 or 0 spaces between digit groups
^\+[0-9]+ *[0-9]{2,} *[0-9]+$ - if there can be 0 or more spaces between digit groups.
Note that $ added at the end of each pattern. Also, see this regex demo.
Related
The strings I parse with a regular expression contain a region of fixed length N where there can either be numbers or dashes. However, if a dash occurs, only dashes are allowed to follow for the rest of the region. After this region, numbers, dashes, and letters are allowed to occur.
Examples (N=5, starting at the beginning):
12345ABC
12345123
1234-1
1234--1
1----1AB
How can I correctly match this? I currently am stuck at something like (?:\d|-(?!\d)){5}[A-Z0-9\-]+ (for N=5), but I cannot make numbers work directly following my region if a dash is present, as the negative look ahead blocks the match.
Update
Strings that should not be matched (N=5)
1-2-3-A
----1AB
--1--1A
You could assert that the first 5 characters are either digits or - and make sure that there is no - before a digit in the first 5 chars.
^(?![\d-]{0,3}-\d)(?=[\d-]{5})[A-Z\d-]+$
^ Start of string
(?![\d-]{0,3}-\d) Make sure that in the first 5 chars there is no - before a digit
(?=[\d-]{5}) Assert at least 5 digits or -
[A-Z\d-]+ Match 1+ times any of the listed characters
$ End of string
Regex demo
If atomic groups are available:
^(?=[\d-]{5})(?>\d+-*|-{5})[A-Z\d_]*$
^ Start of string
(?=[\d-]{5}) Assert at least 5 chars - or digit
(?> Atomic group
\d+-* Match 1+ digits and optional -
| or
-{5} match 5 times -
) Close atomic group
[A-Z\d_]* Match optional chars A-Z digit or _
$ End of string
Regex demo
Use a non-word-boundary assertion \B:
^[-\d](?:-|\B\d){4}[A-Z\d-]*$
A non word-boundary succeeds at a position between two word characters (from \w ie [A-Za-z0-9_]) or two non-word characters (from \W ie [^A-Za-z0-9_]). (and also between a non-word character and the limit of the string)
With it, each \B\d always follows a digit. (and can't follow a dash)
demo
Other way (if lookbehinds are allowed):
^\d*-*(?<=^.{5})[A-Z\d-]*$
demo
Hi trying to create a regex that ensures you have between 1 and 4 number of digits and also as many characters as possible
Here's what I have written so far ^([A-Za-z]+([0-9]){1,4}$)
This doesnt allow me to have characters after the digits
You might repeat the whole part 1-4 times and match optional trailing chars a-z
^(?:[A-Za-z]*[0-9]){1,4}[A-Za-z]*$
The pattern matches
^ Start of string
(?: Non capture group
[A-Za-z]*[0-9]){1,4} Repeat matching 1-4 times optionalchars a-z and a single digit
[A-Za-z]* Optionally repeat char A-Za-z
$ End of string
Regex demo
I want to extract an ID from a search query but I don't know the length of the ID.
From this input I want to get the numbers that are not in the words and the numbers that are not separated by symbols.
12 11231390 good123e41 12he12o1 1391389 dajue1290a 12331 12-10 1.2 test12.0why 12+12 12*6 2d1139013 09`29 83919 1
Here I want to return
12 11231390 1391389 12331 83919 1
So far I've tried /\b[^\D]\d*[^\D]\b/gm but I get the numbers in between the symbols and I don't get the 1 at the end.
You could repeatedly match digits between whitespace boundaries. Using a word boundary \b would give you partial matches.
Note that [^\D] is the same as \d and would expect at least a single character.
Your pattern can be written as \b\d\d*\d\b and you can see that you don't get the 1 at the end as your pattern matches at least 2 digits.
(?<!\S)\d+(?:\s+\d+)*(?!\S)
The pattern matches:
(?<!\S) Negateive lookbehind, assert a whitespace boundary to the left
\d+(?:\s+\d+)* Match 1+ digits and optionally repeat matching 1+ whitespace chars and 1+ digits.
(?!\S) Negative lookahead, assert a whitspace boundary to the right
Regex demo
If lookarounds are not supported, you could use a match with a capture group
(?:^|\s)(\d+(?:\s+\d+)*)(?:$|\s)
Regex demo
I'm using the following expression to validate a house number:
^\d{1,4}([a-zA-Z]{1,2}\d{1,3}|[a-zA-Z]{1,2}|)$
Now the requirement has changed to the following constraints:
one number (25)
one number w/ one letter (25A)
one number w/ a second one divided by a hyphen (25-32)
one number w/ a second one divided by a hyphen and one letter w/ blank (25-32 A)
How do I validate these w/ changes to the regex above?
If you only want to match those values, you might use a pattern to match 1 or more digits followed by an optional part that matches either A-Z OR a hyphen and 1+ digits optionally followed by a space and a char A-Z
^\d+(?:[A-Z]|-\d+(?: [A-Z])?)?$
^ Start of string
\d+ Match 1+ digits
(?: Non capture group
[A-Z] Match a char A-Z
| Or
-\d+ Match
(?: [A-Z])?
)? Close group and make it optional
$ End of string
Regex demo
I am trying to recognize these types of phone number inputs:
0172665476
+6265476393
+62-65476393
+62-654-76393
+62 65476393
While my regex: (?:\d+\s*)+ can recognize the 1st 2 sample values, it recognizes the last 3 sample values as multiple matches in each line, instead of recognizing the number as a whole.
How can I modify this to support multiple dashes and/or spaces and still recognize it as 1 whole number instead of multiple matches?
You may use this regex:
^\+?\d+(?:[\s-]\d+)*\b
RegEx Details:
^\+?: Match optional + at start
\d+: match 1+ digits
(?:[\s-]\d+)*: Match 0 or more groups that start with whitespace or - followed by 1+ digits
$: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)
This should work:
(?:[\d +-]+)+
This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)
Regex: '^(?:[\d +-]+)\b'
Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +, and optional space or hyphen:
(?:\d{10}|\+\d{2}[- ]?\d{3}-?\d{5})\b
That will match:
(?: Non capturing group
\d{10} Match 10 digits
| Or
\+\d{2}[-\s]?\d{3}-?\d{5} Match +, 2 digits, optional whitespace char or -, 3 digits, optional -, 5 digits
)\b Close non capturing group and word boundary
Regex demo
If your language supports negative lookbehinds you could prepend (?<!\S) which checks that what comes before is not a non-whitespace character.