I have seperate regex validations for my requirement but struggling to combine them in one.
I am validation mobile numbers with country code or starting with 00 and also if they contain extension number(2-5 digits) seperated by #
Following is the example of valid Number :
+919986040933
00919986040933
+919986040933#12
+919986040933#123
+919986040933#1234
+919986040933#12345
I have following regex to validate the above:
var phoneRegexWithPlus = "^((\\+)|(00))[0-9]{10,14}$";
var phoneRegexWithZero = "^((\\+)|(00))[0-9]{9,12}$";
var phoneRegexExtension = "^[0-9]{2,5}$";
Currently i am checking whether number contains #,if yes then split it and match number and extension part seperately where extension is comething after hash.
My problem is now that i have to create one regex combining the above three,can anyone help me with that as m not good in regex.
Thanks in advance.
I suggest this expression:
^\+?(?:00)?\d{12}(?:#\d{2,5})?$
See the regex demo
Expression explanation:
^ - start of string
\+? - an optional plus (as ? matches the + one or zero times)
(?:00)? - an optional 00
\d{12} - Exactly 12 digit string
(?:#\d{2,5})? - an optional (again, ? matches one or zero times) sequence of:
# - a literal hash symbol
\d{2,5} - 2 to 5 digits (your phoneRegexExtension)
$ - end of string.
The phoneRegexWithPlus and phoneRegexWithZero are covered with the first obligatory part \+?(?:00)?\d{12} that matches 12 to 14 digits with an optional plus symbol at the start.
NOTE: The regex is adjusted to the sample input you provided. If it is different, please adjust the limiting quantifiers {12} (that can be replaced with, say, {9,14} to match 9 to 14 occurrences of the quantified pattern).
Related
I am trying to write a regular expression for an ID which comes in the following formats:
7_b4718152-d9ed-4724-b3fe-e8dc9f12458a
b4718152-d9ed-4724-b3fe-e8dc9f12458a
[a_][b]-[c]-[d]-[e]-[f]
a - optional 0-3 digits followed by an underscore if there's at least
a digit (if there is underscore is required)
b - 8 alphanumeric characters
c - 4 alphanumeric characters
d - 4 alphanumeric characters
e - 4 alphanumeric characters
f - 12 alphanumeric characters
I have came up with this regexp but I would appreciate any guidance and/or corrections. I am also not too sure how to handle the optional underscore in the first segment if there are no digits up front.
/([a-zA-Z0-9]{0,3}_[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12})+/g
Your regex looks good. To optionally match the first 3 digits with an underscore, you can wrap that group with ()?. Also you can force the presence of a digit before the underscore by using {1,3} instead of {0,3}.
Unless you expect that multiple identifiers are following each other without space and should be matched as one, you can drop the last + (for multiple matches on the same line, you already have the g option).
The final regex is ([a-zA-Z0-9]{1,3}_)?[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}
See here for a complete example.
If you also do not need to capture the individual 4-alphanumeric groups, you can simplify your regex into:
([a-zA-Z0-9]{1,3}_)?[a-zA-Z0-9]{8}-([a-zA-Z0-9]{4}-){3}[a-zA-Z0-9]{12}
See here for an example.
I have a couple of regex which I am planning to combine.
So the first regex is as below (allows amounts with particular thousand and decimal separators)
"^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"
I have similar other regexes (based on different locales e.g. other one would have comma as the decimal separator)
So with the above regex, following are Valid/Invalid values
123.11 (Valid)
1'23 (Invalid)
With the second regex, I want that the string can contain a max of 13 digits (including before or after the decimal)
^[^\\d]*?(\\d|\\d[^\\d]+){0,13}$
With the above regex, following are Valid/Invalid values
1234567890123 (Valid - 13 digits)
12345678901234 (Invalid - 14 digits)
1234567890.123 (Valid as 13 digits...10.3)
1234567890.1234 (Invalid as 14 digits...10.4)
Is it possible to somehow consolidate the 2 regex?
However, I do not want to touch the first regex (have different combinations based on different locales). But it would be nice to somehow dynamically append the 2nd regex into the first one ?
So, I am flexible with the 2nd regex as that is not based on any locale, but is going to be the same always and mainly validates for max of 13 digits in the string.
I'll then validate my string using the consolidated regex.
You may keep the first pattern as is, and just prepend it with
(?=^\D*(?:\d\D*){0,13}$)
The (?=^\D*(?:\d\D*){0,13}$) pattern represents a positive lookahead that matches a location that is immediately followed with
^ - start of string
\D* - 0+ non-digits
(?:\d\D*){0,13} - 0 to 13 occurrences of a digit followed with a non-digit char
$ - end of string.
Full JavaScript regex definition:
var regex1 = "^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"; // Not to be touched
var consolidated_regex = "(?=^\\D*(?:\\d\\D*){0,13}$)" + regex1;
See full regex demo.
Details
Here is my pattern. I'm trying to allow numbers and a decimal with two places plus an optional comma with three digits.
var pattern = /^[0-9]+(,\d{3})*\.[0-9]{2}$/;
Allow
100,000.12
10,000.12
1,000.12
100.12
10.12
.12 (can't get this to allow... see below)
Don't allow
abcd
1,,000.12
1,00.12
1,000.0
1,000.
1,000
Here is the test. If I add a ? after [0-9] it works here, but it does not work in my MVC 5 View. The modal doesn't open, so MVC doesn't like it.
^[0-9]?+(,\d{3})*\.[0-9]{2}$
https://regex101.com/r/HwLS7q/1
UPDATE 1
Don't allow
000,000.12, 0.12 etc...
Any help is much appreciated! Thanks!
[0-9]?+ is a pattern that matches 1 or 0 digits possessively, not allowing backtracking into the pattern. JS regex does not support possessive quantifiers, hence the issue.
You need to use
^[0-9]*(?:,[0-9]{3})*\.[0-9]{2}$
Or
^(?:[0-9]+(?:,[0-9]{3})*)?\.[0-9]{2}$
Here, the [0-9]* match zero or more digits and (?:[0-9]+(?:,[0-9]{3})*)? matches an optional sequence of 1+ digits followed with 0+ repetitions of , and 3 digit groups.
See this regex demo.
A more precise pattern would be to restrict the first digit chunk to 1, 2 or 3 digits and make the integer part optional:
^(?:[0-9]{1,3}(?:,[0-9]{3})*)?\.[0-9]{2}$
See the regex demo.
Details
^ - start of string
(?:[0-9]{1,3}(?:,[0-9]{3})*)? - an optional sequence of
[0-9]{1,3} - one to three digits
(?:,[0-9]{3})* - 0 or more repetitions of
, - comma
[0-9]{3} - three digits
\. - dot
[0-9]{2} - two digits
$ - end of string.
I have a regex that i ended up using from one of the answer here in SO .
Basically my regex must validate ipv4 address with mask .
So i ended up using the below regex :
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254))))
Now my challenge is to not allow 0 in the last digit of ip i.e ,
192.168.6.10/mask is valid but 192.168.6.0/mask is invalid
So i modified the above regexp to something like this :
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[1][0-9][0-9]|[1-9][0-9]|[1-9]?)/([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254))))
but 192.168.6.0 is always valid when testing with Angular Validators.pattern
Any idea where i'm going wrong ?
EDIT
List of IPs & its validity :
192.168.6.6/24 is valid
192.168.6.6/24 is valid
192.168.6.24/24 is valid
192.168.6.0/24 invalid
192.168.6.0/255.255.255.0 is invalid
You want to avoid matching any IP with the last octet set to 0.
You may use
ipAddress : FormControl = new FormControl('' , Validators.pattern(/^(?!(?:\d+\.){3}0(?:\/|$))(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:[1-9]|1[0-9]|2[0-9]|3[0-2]|(?:(?:128|192|224|240|248|252|254)\.0\.0\.0|255\.(?:0|128|192|224|240|248|252|254)\.0\.0|255\.255\.(?:0|128|192|224|240|248|252|254)\.0|255\.255\.255\.(?:0|128|192|224|240|248|252|254)))$/));
Here is the regex demo
The main addition is the lookahead after ^ that is executed once at the start of a string. The (?!(?:\d+\.){3}0(?:\/|$)) pattern is a negative lookahead that fails the match if, immediately to the right of the current location (string start), there are:
(?:\d+\.){3} - three repetitions of 1+ digits and a dot
0 - a zero
(?:\/|$)) - / or (|) end of string ($).
Notice I defined the pattern using a regex literal notation (/regex/) and I had to add ^ (string start) and $ (string end) anchors since the regex was no longer anchored by default. Also, to escape special chars in a regex literal notation, you only need one backslash, not two.
Suppose that the last part cannot be written 000 and 00 but just 0. Then you can you such regex
^(?:(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)\.){3}(?:(?:2(?:5[0-5]|[0-4]\d)|1?\d\d|[1-9]))$
Where diff between the first groups and the last one that one-digit value should be from 1 to 9
demo
You can try with this pattern
^(?:[1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:[1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:[1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:2[0-5][1-5]|[1-9]|1[0-9][1-9]|[1-9][1-9])$
Online demo
For the last numbers you have check with this
(?:2[0-5][1-5]|[1-9]|1[0-9][1-9]|[1-9][1-9])
One possible approach here is simple, and just involves adding a negative lookbehind at the very end of the pattern (?<!\.0), which asserts that .0 is not the immediately preceding term in the IP address. Applying this to your correctly working pattern from the comments above, we get:
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/
([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|
(255\.(0|128|192|224|240|248|252|254)\.0\.0)|
(255\.255\.(0|128|192|224|240|248|252|254)\.0)|
(255\.255\.255\.(0|128|192|224|240|248|252|254))))(?<!\.0)$
Demo
The downside is that your JavaScript engine may not yet support negative lookbehind syntax just yet.
So, js apparantly doesn't support lookbehind.
What I want is a regex valid in javascript that could mimic that behavior.
Specifically, I have a string that consists of numbers and hyphens to denote a range. As in,
12 - 23
12 - -23
-12 - 23
-12 - -23
Please ignore the spaces. These are the only cases possible, with different numbers, of course.
What I want is to match the first hyphen that separates the numbers and is not a minus sign. In other words, the first hyphen followed by a digit. But the digit shouldn't be part of the match.
So my strings are:
12-23
12--23
-12-23
-12--23
And the match should be the 3rd character in the 1st 2 cases and the 4th character in the last two.
The single regex I need is expected to match the character in brackets.
12(-)23
12(-)-23
-12(-)23
-12(-)-23
This can be achieved using positive lookbehind :
(?<=[0-9])\-
But javascript doesn't support that. I want a regex that essentially does the same thing and is valid in js.
Can anyone help?
I don't know why you want to match the delimiting hyphen, instead of just matching the whole string and capture the numbers:
input.match(/(-?\d+) *- *(-?\d+)/)
The 2 numbers will be in capturing group 1 and 2.
It is possible to write a regex which works for sanitized input (no space, and guaranteed to be valid as shown in the question) by using \b to check that - is preceded by a word character:
\b-
Since the only word characters in the sanitized string is 0-9, we are effectively checking that - is preceded by a digit.
(\d+.*?)(?:\s+(-)\s+)(.*?\d+)
You probably want this though i dont know why there is a diff between expected output of 2nd and 4th.Probably its a typo.You can try this replace by $1$2$3.See demo.
http://regex101.com/r/yR3mM3/26
var re = /(\d+.*?)(?:\s+(-)\s+)(.*?\d+)/gmi;
var str = '12 - 23\n12 - -23\n-12 - 23\n-12 - -23';
var subst = '$1$2$3';
var result = str.replace(re, subst);