Regex 1964 to 2007 [duplicate] - regex

This question already has answers here:
Convert integer numeric interval to regex
(3 answers)
Closed 8 years ago.
In PHP, I know that /^((19|20)\d\d)$/ will match all numbers from 1900 to 2099.
What is the most elegant way to match all numbers from 1964 to 2007 ?
/^(196[456789]|19[789]\d|200[01234567])$/ ?
Edit:
Many answers to similar questions recommend using comparison instead of regex, for example because the regex is hard to read and difficult to maintain.
The problem with comparisons is that strings that are not numbers might match. For example:
$num = "";
if($num >= 0 && $num < 10) echo "yes"; // yes
Obviously an empty string is not a number between 0 and 10. So an easily maintainable comparison needs to check for numericity as well, which might make it less superior to a regex.

I would say /^19(6[4-9]|[7-9]\d)|200[0-7]$/ is the most elegant way.

^(196[456789])|(197[0-9])|(198[0-9])|(199[0-9])|(200[0-7])$

Related

Regular expression for value incease [duplicate]

This question already has answers here:
Regex multiplication
(2 answers)
Closed 2 years ago.
Is there possible with reg ex to find a string, say xxVAR1, xxVAR2, xxVAR3, where VARx is a number and for each match increase VARx say by 20%?
No, regex is pattern finding system. It cant calculate by its own. What you can do is find the first one by regex, then extract, calculate in your programming language and create a new regex. But there could be better ways around this task. Regex has no understanding of the math in numbers, for it every number is just a character that could be found.
You cannot exactly achieve what you want using regex. You have to use regex in addition to some high level language.
You can use this regex as a base
/(..VAR[0-9]{1})/
You can use Python or language of your choice to achieve following
regex = '/(..VAR[0-9]{%num%})/'
for i in range(1, 5):
new_regex = regex.replace('{%num%}', str(i))
# Do whatever you want

Regex to match only if symbol is found at most once in fixed length pattern [duplicate]

This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 3 years ago.
I need help with a regex that should match a fixed length pattern.
For example, the following regex allows for at most 1 ( and 1 ) in the matched pattern:
([^)(]*\(?[^)(]*\)?[^)(]*)
However I can not / do not want to use this solution because of the *, as the text I have to scan through is very large using it seems to really affect the performance.
I thus want to impose a match length limit, e.g. using {10,100} for example.
In other words, the regex should only match if
there are between 0 and 1 set of parenthese inside the string
the total length of the match is fixed, e.g. not infinite (No *!)
This seems to be a solution to my problem, however I do not get it to work and I have trouble understanding it.
I tried to use the accepted answer and created this:
^(?=[^()]{5,10}$)[^()]*(?:[()][^()]*){0,2}$
which does not seem to really work as expected: https://regex101.com/r/XUiJZz/1
Also please do not mark this question a duplicate of another question, if the answers in that question make use of the kleene star operator, it wont help me.
Edit:
I know this is a possible solution, but I'm wondering if there is a better way to do it:
([^)(]{0,100}\(?[^)(]{0,100}\)?[^)(]{0,100})
I thus want to impose a match length limit, e.g. using {10,100}
You may want to anchors add a lookahead assertion in your regex:
^(?=.{10,100})[^)(]*(?:\(?[^)(]*\))?[^)(]*$
(?=.{10,100}) is lookahead condition to assert that length of string must be between 10 and 100.
RegEx Demo

Regular expression for range of numbers [duplicate]

This question already has answers here:
Regular Expression: Numeric range [duplicate]
(9 answers)
Using regular expressions to validate a numeric range
(11 answers)
Closed 8 years ago.
Can somebody help me with this regular expression?
Numbers that are anything from 100 to 9999. Excluding 112, 144 and the whole 900 - 999 range.
This is a great opportunity to not use regular expressions at all. You are interested in the value of the numbers not their textual format so just convert the value into an integer ( if the conversion fails you have bad input ) and then perform a numerical analysis on it.
This will be easier, more readable and very probably perform better than using a regular expression.
You can use this regex if regular script/language constructs don't work for you:
^(?!(9[0-9]{2}|112|144)$)[1-9][0-9]{2,3}$

is it possible to do Math on Variable Values in Regex? [duplicate]

This question already has answers here:
Math operations in regex
(3 answers)
Closed 8 years ago.
i am wondering of it is possible to do some simple Math on RegEx Variable values.
E.G.:
I am looking for all two-digit numbers is a textfile and would like to multiply them by 10.
Is simple regex able to do this or do i need to use a more complex script for that?
thanks!
Multiply two-digits number is like appending 0 at the end of the numbers. So that can be done with any regular expression that support replace and capturing group.
For example, here is Python code:
>>> re.sub(r'\b(\d{2})\b', r'\g<1>0', 'There are 10 apples.')
'There are 100 apples.'
But what you want is multiply by arbitrary number, then you need the regular expression engine that support some kind of callback / evaluation.
>>> re.sub(r'\b(\d{2})\b', lambda m: str(int(m.group(1)) * 5), '10 apples.')
'50 apples.'

Regex to match one of lengths, but nothing in between [duplicate]

This question already has answers here:
Regex exactly n OR m times
(6 answers)
Closed 8 years ago.
When I want to test if input string is at least length 10 and at most length 13, I use this pattern: [0-9]{10,13}
But how would you test if the string length is 10 or 13 and nothing in between?
EDIT: I see two dominant solutions:
1) Using length1 OR length2
2) Using length1 + optional characters of (length2 - length1)
Just wondering: are there any performance differences between the two?
Another option would be
^[0-9]{10}(?:[0-9]{3})?$
regex101 demo
use this expression ^((?:\d{3}){3,4}\d)$