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
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 need to extract certain part of Javascript string. I was thinking to do it with regex, but couldn't come up with one which does it correctly.
String can have variable length & can contain all possible characters in all possible combinations.
What I need to extract from it, is 10 adjacent characters, that match one of next two possible combinations:
9 numbers & 1 letter "X" (capital letter "X", not X as variable letter!)
10 numbers
So, if input string is this: "[1X,!?X22;87654321X9]ddee", it should return only "87654321X9".
I hope I've explained it good enough. Thanks in advance!
This Regex will work:
\d{9}X|\d{8}X\d|\d{7}X\d{2}|\d{6}X\d{3}|\d{5}X\d{4}|\d{4}X\d{5}|\d{3}X\d{6}|\d{2}X\d{7}|\d{1}X\d{8}|\d{10}|X\d{9}
As described, It need to match 9 digits and any letter, and the letter can be at any position of the sequence.
\d{9}X # will match 9 digits and a letter in the end
\d{8}X\d # will match 8 digits a lettter then a digit again
...
\d{1}X\d{8} # will match 1 digits a lettter then 8 digits
\{10} # will match 10 digits
Edited to match only X
You can use this much simpler regex:
/(?!\d*X\d*X)[\dX]{10}/
RegEx Breakup:
(?!\d*X\d*X) # negative lookahead to fail the match if there are 2 X ahead
[\dX]{10} # match a digit or X 10 times
Since more than one X is not allowed due to use of negative lookahead, this regex will only allow either 10 digits or ekse 9 digits and a single X.
RegEx Demo
This regex has few advantages over the other answer:
Much simpler regex that is easier to read and maintain
Takes less than half steps to complete which can be substantial difference on larger text.
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).
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);
I'm using this /[-\+,\.0-9]+/ to match numbers in strings like +4400,00 % or -3500,00 % or 0.00 %.
The matched results I want is +4400,00 and I correctly get it.
What if I wanted the same results for a string like +4.400,00 % (dot for thousands) ?
EDIT
How do I have to modify my RegEx for matching numbers in strings like <font color="red">+44.500 %</font>?
/[\-\+]?\s*[0-9]{1,3}(\.[0-9]{3})*,[0-9]+/
That should cover strings that
may start with a + or -, and then perhaps some whitespaces
then have between one and three numbers
then have groups of three numbers, prefixed with a period
then have a comma and at least one number behind the comma
Regarding your additional question (matching numbers inside strings), you should look into the manual of whatever regex API you're using. Most APIs have separate search and match methods; match wants the whole string to be part of your regular expression's language, while search will also match substrings.
[\+-]? - plus or minus
\d{1,3} - some digits
(\.\d{3})* - groups of 3 digits with point before
,\d{2} comma and 2 more digits
And so we get:
/[+-]?\d{1,3}(\.\d{3})*,\d{2}/
Your regex will already match ".". But it sounds like you also want to strip "." out? if that's the case, you need a substiution. In Perl,
if ($input =~ /(-|\+)[0-9][,\.0-9]+/) {
$input =~ s/\.//;
} else {
die;
}
I've also changed the regex so it will only match - and + at the start, and so it requires an initial digit