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)
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 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.
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:
Regular expression for a string containing one word but not another
(5 answers)
Closed 2 years ago.
I lack experience with regex and need help with this problem:
Objective: Create a regex pattern that matches "renal" or "kidney" in an arbitrary string only if it does not contain "carcinoma".
Example strings:
"Renal cell carcinoma"
"Clear cell carcinoma of kidney"
"Chronic renal impairment"
Expected output: The regex pattern does not match "renal" and "kidney" in the first two strings; it does match "renal" in the third string (since there is no "carcinoma").
What I've tried: (?<!carcinoma).*(kidney|renal). I stopped here because it didn't work — because, as I've learned here and here, lookbehinds are limited to basically non-zero length; regular expressions cannot be applied backwards an arbitrary length.
So what regex pattern will do the trick? I want a pattern that maintains focus on (or is "anchored" on) "renal" and "kidney" and not "carcinoma".
The pattern that you tried (?<!carcinoma).*(kidney|renal) asserts what is directly to the left is not carcinoma which is true from the start of the string.
Then it will match any char 0+ times until the end of the string and tries to backtrack to fit in either kidney or renal.
Instead of using (?<!carcinoma), use ^(?!.*\bcarcinoma\b) to assert from the start of the string that bcarcinoma is not present at the right.
Then match either the word renal or kidney in the string.
^(?!.*\bcarcinoma\b).*\b(renal|kidney)\b.*
Regex demo
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})