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

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.'

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 value of 0.0-5.0 [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 5 years ago.
I'm trying to create a regex string that allows values 0.0 - 5.0. I need the one decimal point to be required. The string below gets me there, but it also allows 5.1-5.9. How do I prevent 5.1-5.9 from being entered, and allow 5.0?
^[0-5]+(\.[0-9]{1})$
Try this regex:
^([0-4]\.[0-9]|5\.0)$
It matches any number from 0 to 4 then dot then any number.
it also matches 5.0
Note: Your regex has another problem that you used + after [0-5] which also matches 55 for example, so you need to remove the +. You also need to remove {1}, It won't make any change but it's useless.

Validate time and allow negative values [duplicate]

This question already has answers here:
How do I include negative decimal numbers in this regular expression?
(17 answers)
Closed 6 years ago.
I use this regex to validate time entries, e.g. 08:34 - this is a valid time,
negative time e.g. -08:54 is not valid with this regEx.
Actually I don't know how to extend this regex in order to make also negative times valid:
var regex_time =/^([0-9]{2})\:([0-9]{2})$/;
You can use the regex:
^-?([01]\d|2[0-3]):([0-5]\d)$
To get a positive/negative valid 24h format. If you don't need to limit the user to 24h format, use:
^-?(\d+):([0-5]\d)$
The question mark is a quantifier that means 0 to 1 of previous item.

RegEx to get numeric value and values with range [duplicate]

This question already has answers here:
How to write regex to match number ranges?
(7 answers)
Closed 8 years ago.
I need a RegEx to match the following format:
3
7.3
5.7 (2.2-10.4)
I want to check in JS if the given value i.e. 5.7 (2.2-10.4) is valid. And in PHP I want to split the string to get all three values (if there is a given range).
That means I have to check for a simple numeric value (int and float) or a numeric value followed by a range like (d-d)
My attempt: /^(\d+)\s\((\d+)\-(\d+)\)$/
based on your attempt, does not count for negative values
^(\d+(?:\.\d+)?)(?:\s*\((\d+(?:\.\d+)?)-(\d+(?:\.\d+)?)\))?$
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}$