If Else regex matching [duplicate] - regex

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})

Related

Regular expression: Should not start with 5 digits (or more) in a row [duplicate]

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

Regex: Exclude characters from right in the string until numeric is found [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have a string in the following format:
ABC12233434343DEF
How can I extract only:
ABC12233434343
I want to leave out the ending set of characters of whatever length they might be.
There are several ways this is one:
.*?\d+
It will match anything at the beginning that is followed by numbers.
It may be also posible to limit the characters it can match initially, like if you was capital letters from A-Z, for example:
[A-Z]+\d+
Online Demo

How to put condition in Regex with multiple patterns? [duplicate]

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.

Regular expression to match ) under certain conditions [duplicate]

This question already has answers here:
Regex for matching something if it is not preceded by something else
(3 answers)
Closed 2 years ago.
I am trying to match ) if there is a ( and two numbers to the left of it.
Example "(55)"
I want to match ) in "(55)"
I do not want to match "(hello world 55)"
I currently have the following as my regex:
\(\d+\)
It matches "(55)" but I just want the ) in it. Is there a way to get a certain character by placement in a regex? Or do you have a better solution?
There are multiple ways to match what you want.
For instance, you can use \K to rest the previous match:
\(\d+\K\)
Using positive lookbehind
(?<=\(\d+)\)
Also capturing the content for the match
\(\d+(\))
Flutter and all Languages supported
Insert all Special Character, Number, Lowercase & Uppercase
condition 1: Must Length of word 8
condition 2: Insert any of above given list (Not Must in all type)
r'^([a-zA-Z].)*.{8,}$' ,
Must Insert Atleast One in Above given list,
r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+!##\$&*~{8,}$',

RegEx for capturing digits with an optional comma [duplicate]

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: