This question already has answers here:
Regex for Comma Separated Number or Just Number
(5 answers)
Closed 3 years ago.
I'm scraping this from some site and from this text " See all {a number will be there} employees".
Now this number is separated by "," as in 1,200. So what will be the regex to generalize as it could be 200 or 1,200 or 12,345,098.
Attempt
[0-9]+\.[0-9]+
This expression would simply output our desired number:
([0-9,]+)
or
(\d+,?)
which is a capturing group, including any digit and any optional comma. If necessary, we could bound it more if we wish to.
DEMO 1
DEMO 2
Advice:
The fourth bird:
I think repeating the comma and the digit would be more precise like
for example \d+(?:\,\d+)* or else in a text first pattern can also
match a separate comma and only match separate groups with the second.
RegEx Circuit
jex.im visualizes regular expressions:
Related
This question already has an answer here:
Regular expression for a string that does not start with a sequence
(1 answer)
Closed 1 year ago.
I need to generate a regular expression to validate that the string does not start with 5 digits.
NOT VALID: 12345testing123asd
VALID: 1234testing1234
testing12345
testing
I tried to get the first five chars ^.{0,5} but I do not know hot to add the restriction of \D to those first 5 chars
Also, I tried with this [^0-9][^0-9][^0-9][^0-9][^0-9] but I do not know how to do to include the strings that starts with 4 or less numbers
Could you please help me with this? I am a rookie :(
If your RegExp flavor of choice supports negative lookaheads, this pattern will match if the string is valid (does not start with 5 or more consecutive digits):
^(?!\d{5,})
Regex101
Matches:
1234testing1234
testing12345
testing
Does not match:
12345testing123asd
This question already has answers here:
Combine Regexp?
(6 answers)
Closed 2 years ago.
How can I incorporate these two regex patterns in one statement with conditions if
the first three numbers are 123, then regex2 is applicable and if not equal to
123, then regex1 is applicable?
Regex1 "^C\sN\+\d{10,12}\/?(EN|FR)?$"
Value 1: CTC N+6534567890/FR
Regex2 "^C\sN\+\d{12}\/?(EN|FR)?$"
Value 2: CTC N+123456789012/FR
You may use:
^CTC\sN\+(?:123\d{9}|\d{10,12})\/?(EN|FR)?$
Demo.
The relevant part (i.e., (?:123\d{9}|\d{10,12})) will match either "123" followed by 9 digits (for a total of 12 in Regex2) or between 10 and 12 digits (from Regex1).
Note that unless you want to capture "EN" and "FR" separately, you may convert the capturing group into a non-capturing one (i.e., (?:EN|FR)).
One more thing to be aware of is that if the number starts with "123" and has a length <12 (10 or 11), it will still be a valid match (because it satisfies the \d{10,12} part from Regex1). If you need to prevent that (and assuming your regex flavor supports Lookaheads), you may use:
^CTC\sN\+(?:123\d{9}|(?!123)\d{10,12})\/?(EN|FR)?$
Demo.
References:
Alternation in Regular Expressions.
Non-capturing groups.
This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 2 years ago.
I need a regex to validate phone number without plus (+) sign for example
46123456789,46-123-456-789,46-123-456-789
number should be 11 digit rest of should ignore
i am currently using this Regex /([+]?\d{1,2}[.-\s]?)?(\d{3}[.-]?){2}\d{4}/g
its not correct at all
About the pattern you tried:
Using this part in your pattern [+]? optionally matches a plus sign. It is wrapped in an optional group ([+]?\d{1,2}[.-\s]?)? possibly also matching 12 digits in total.
The character class [.-\s] matches 1 of the listed characters, allowing for mixed delimiters like 333-333.3333
You are not using anchors, and can also possible get partial matches.
You could use an alternation | to match either the pattern with the hyphens and digits or match only 11 digits.
^(?:\d{2}-\d{3}-\d{3}-\d{3}|\d{11})$
^ Start of string
(?: Non capture group for the alternation
\d{2}-\d{3}-\d{3}-\d{3} Match either the number of digits separated by a hyphen
| Or
\d{11} Match 11 digits
) Close group
$ End of string.
Regex demo
If you want multiple delimiters which have to be consistent, you could use a capturing group with a backreference \1
^(?:\d{2}([-.])\d{3}\1\d{3}\1\d{3}|\d{11})$
Regex demo
I would have this function return true or false and use as is.
function isPhoneValid(phone) {
let onlyNumbers = phone.replace(/[^0-9]/g, "");
if (onlyNumbers.length != 11) console.log(phone + ' is invalid');
else console.log(phone + ' is valid');
}
isPhoneValid('1 (888) 555-1234');
isPhoneValid('(888) 555-1234');
I am not sure how is the input looks like. But based on your question I supposed you want to trim it and match it with regex?
trim your input.
string.split(/[^0-9.]/).join('');
and you can match it with this regex:
((\([0-9]{3}\))|[0-9]{3})[\s\-]?[\0-9]{3}[\s\-]?[0-9]{4}$
This question already has answers here:
regular expressions: match x times OR y times
(4 answers)
Closed 3 years ago.
I want to build a regex where it searches for a string containing 12 digits in a row. If there's no match, look for a string with only 10 digits in a row.
For example:
a123456789012a
a1234567890a
Would return:
123456789012
And if the input is:
a1234a
a1234567890a
It would return:
1234567890
I managed to create the regex for the individual operations, beeing (?<!\d)\d{10}(?!\d) for 10 digits and (?<!\d)\d{12}(?!\d) for 12 digits, but I can't group them up in a if-else style.
I tried the following:
(?(?<!\d)\d{12}(?!\d)|((?<!\d)\d{10}(?!\d)))
but if the first pattern don't match, the regex don't try to match the second, returning nothing
You can use a simple regex like this:
\d{12}|\d{10}
working demo
Look that I have not used multiline nor global flags. This way the pattern is going to find the first match you want.
Case 1:
Case 2:
BTW, use capturing groups if you want to capture the content:
(\d{12}|\d{10})
This question already has answers here:
Validate if input string is a number between 0-255 using regex
(12 answers)
Closed 5 years ago.
Is there a way to match a range of numbers(0-255) without the ^ and $?
Matched numbers
1
12
123
Not matched numbers
1234
555
You can use lookahead and lookbehind to match only 1-3 digits.
(?<!\d)(?:[1-9]?\d|1\d\d|2(?:[0-4]\d|5[0-5]))(?!\d)
Regex101 demo
Not using anchor characters, BrightOne was wise to integrate lookarounds for numeric characters. However, the pattern isn't fully refined. To optimize the pattern for speed and maintain accuracy:
Use quantifiers on consecutive duplicate characters.
Organize the alternatives not in sequential order but with quickest mis-matches first.
Avoid non-essential capture (or non-capture) groups. (despite seeming logical to condense the "two hundreds" portion of the pattern)
This is my suggested pattern: (Demo)
/(?<!\d)(?:1\d{2}|2[0-4]\d|[1-9]?\d|25[0-5])(?!\d)/ #3526 steps
(Brightone's pattern resolves in 5155 steps)
(treesongs' second pattern resolves in 5184 steps) *at time of posting, the first pattern was inaccurate)