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

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.

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

meaning of this the regex with a - (minus sign) in this specific regex [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
((\d\d\d)\s)?\d\d\d-\d\d\d\d
just wondering what would be the meaning of above?
I think the first part ((\d\d\d)\s)? means something like this:
352k (since \d means random digit, \s means a non-space character, so I just use k here, and ? here means 352k repeated 0 or 1 time, so I have it 1 time here)
Then I am not quite sure what is the meaning of the thing that follows it i.e. \d\d\d-\d\d\d\d
I don't get the - (the minus sign) here and how it relates the first 3 digits, and last 3 digits in this expression here.
P.S. I tried this here (they don't work):
(345) 578-3291
(382) 441-2219
or 341 319-3183
or 321 999-1318
so phone number pattern doesn't work. Does anyone know?
You should find the answers you're looking for in an introduction to regular expressions, such as https://medium.com/better-programming/introduction-to-regex-8c18abdd4f70. If you want to fiddle with the expression in order to get a better understanding of how it works there are numerous online regex testers (eg https://www.regextester.com/).
One important note is that regexes have a long history and come in different flavours, which often have incompatible syntax.
A rough outline of the regex you have quoted is:
- An optional 3 digits and space, followed by
- 3 more digits, then
- a hyphen character, then
- then 4 more digits.
The brackets have the function of grouping the optional section, as well as allowing you to extract the resulting substrings, so if you applied this regex to the string 123 456-7890 then group 1 would contain 123 and group 2 would contain 123. (Group 0 always contains the whole matched string). Alternatively, regex would also match 456-7890 and in this case the capture groups would both be empty.

If Else regex matching [duplicate]

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

Regular expression to match a range 0-255 without ^ and $ [duplicate]

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)

noncapturing group explanation within a positive lookahead [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
Does this regular expression mean that at least one of the following that isn't a-z:
(?=.*(?:[a-z]))
It's part of the following expression:
/^(?=[A-Za-z0-9\'\s\d\.]{2,50}$)(?=.*(?:[a-z]))[a-zA-Z0-9]+[A-Za-z0-9\'\s\.]+$/m
No, (?=.*(?:[a-z])) means that there could be whatever but must finish with a lowercase letter.
This regex means:
/^(?=[A-Za-z0-9\'\s\d\.]{2,50}$)(?=.*(?:[a-z]))[a-zA-Z0-9]+[A-Za-z0-9\'\s\.]+$/m
Match the line that starts with 2 to 50 alphanumeric, single quote, spaces or a dot, and then follows with lower case letter, and continues with alphanumerics and must ends followed by alphanumerics, spaces, single quote or dot.
Here you can see a better graphical approach for your regex:
Actually, this can be improved as:
/^(?=[A-Za-z\d'\s.]{2,50}$)(?=.*[a-z])[a-zA-Z\d]+[A-Za-z\d'\s.]+$/m