Regular expression for value incease [duplicate] - regex

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

Related

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 Expressions AND [duplicate]

This question already has answers here:
Regular Expressions: Is there an AND operator?
(14 answers)
Closed 6 years ago.
I am really not good with regular expressions and I come here for some assistance :). I am trying to combine regular expressions with something like AND. For example if we have a text file with:
abc1-xyz
abc1-ertxyz
abc1xyz
postxyz
abc1
I would like to match everything that starts with "abc1" AND also contains the letters "xyz" somewhere.
I know that I can start with:
/^abc1/
but I am not sure how to combine so it can also match to contain "xyz".
Thank you for your assistance in advance.
You should tell us with which language you are coding, regex engines are not always the same.
There is another ambiguous point : Do you need your string to CONTAIN xyz or to END WITH?
Considering you are coding on Javascript..
If you want it to contain xyz, try :
/^abc1.*xyz/
If you want it to end with xyz, try :
/^abc1.*xyz$/

Regular expression to match floats only [duplicate]

This question already has answers here:
Matching numbers with regular expressions — only digits and commas
(10 answers)
regular expression for finding decimal/float numbers?
(9 answers)
Closed 7 years ago.
I need to do a regular expression to match the floats only, what i got is the following :
[\-\+]?[0-9]*(\.[0-9]+)?
But this match also the below
123123132 ,
05/03/1994
I only need want to match the number with the decimal point
Your regex is almost correct for your purpose.
It finds 123123132, because the last part is optional. Removing the ? solves that.
[-+]?[0-9]*(\.[0-9]+)
With that adjustment, it might still find matches in strings like .12/39/3239, if you don't want that to happen, insert enforce matching over the complete string by inserting ^ and $:
^[-+]?[0-9]*(\.[0-9]+)$
How about:
([+-]?[0-9]*\.[0-9]*)
You can see it working here
Here is a regexp handling also existing exponents:
[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?
Debuggex Demo
Additionally you should force the hole string to be matched to avoid matchings within your date values.
^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$
By the way here is a nice tutorial about matching floating point numbers using regular expressions: http://www.regular-expressions.info/floatingpoint.html.

Detecting if two regexes could possibly match the same string [duplicate]

This question already has answers here:
Regex: Determine if two regular expressions could match for the same input?
(5 answers)
Closed 8 years ago.
Given two regular expressions, is it possible to detect whether there is any possible string that matches them both?
For example, given regexes A and ., I can see that string "A" matches them both. That's a simple case.
My question is for the broader case -- given any two valid regexes, would it be possible to definitively say whether there is any possible string that would match both regexes? Assume that there is no sample set of input strings to test. All I have are the regexes. I don't necessarily need to produce matching strings -- I just need to determine that there are possible strings that match both.
Will accept discussions for any of the common regex specifications -- .NET, Java, PERL, sed, grep, etc.
Basically, you want to test if the intersection of two RegExps is non-empty. Since intersection - just like complement - is a potentially expensive operation (it requires determinization of the NFA), it is not implemented in many RegExp implementations. One exception I know of is the BRICS Automaton Library, which allows enabling the intersection operator &.
To test the property in question, you could use the BRICS (Java) library like this:
RegExp re = new RegExp("(.) & (a)", RegExp.INTERSECTION); // Parse RegExp
Automaton a = re.toAutomaton(); // convert RegExp to automaton
if(a.isEmpty()) { // Test if intersection is empty
System.out.println("Intersection is empty!");
}
else {
// Print the shortest accepted string
System.out.println("Intersection is non-empty, example: " + a.getShortestExample(true));
}
Yes, it's possible theoretically.
But it basically comes down to try all possible options and see which matches both regexes. But it's more a theoretical computer science question, with modern day regular expressions in programming languages this would be a problem in NP (http://en.wikipedia.org/wiki/NP_(complexity))
If you're talking more about the formal language theory definition of a regular language, than I would say it should be possible by converting both regexes to a DFA and walk through both simultaneously to see what would match.

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