This question already has answers here:
Regular expression for math operations with parentheses
(4 answers)
Closed 2 years ago.
I need help to build a regular expression that accepts the basic arithmetic operations algorithm, but also allows meter operations on any number of parentheses
so far I have this expression:
^([(]*(-)?\d+(\.\d+)?[)]?)([(]?[-+/*%^]?\d+(\.\d+)?[)]*)+
It happens that the above expression accepts me without closing parenthesis or unopened (parentheses must go in pairs).
I show the evidence that I have made, which is in the red box should not accept
http://regexr.com/38r4u
And I hope you can help me,
Thanks.
You cannot parse a recursive structure using a regex. Use a parser instead.
Related
This question already has answers here:
How to check that a string is a palindrome using regular expressions?
(32 answers)
Closed 3 years ago.
I am not using any programming language in particular, rather this is a University regular language assignment I have to do.
To explain what I have to do is basically make an:
infinite language
Words must contain any number a's and b's, so no single character words
words are palindromes to themselves so for example words I should get are "aabbaa","abba", "abbbba", "ababbaabbaba", "bbbaabbb"
How would I approach making this regular expression?
One choice might be then,
ab+ba
or
ab*ba
since single chars are not allowed.
These expressions would simply meet the infinite set and palindrome criteria.
RegEx Demo 2
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
RegEx Circuit
jex.im visualizes regular expressions:
This question already has answers here:
How to find overlapping matches with a regexp?
(4 answers)
Closed 3 years ago.
I'm trying to extract the repeated pattern from a string.
For example with something like "112112112112" I would want to end up with "112".
I've been having problems where I either end up with "1" or "112112".
The patterns can be of any size.
Here's an example of the kind of expressions I've been playing around with.
^(.+)(?=\1)
There are repeated patterns with different sizes, if 3 would be desired, for instance, we'd use a quantifier for that, such as:
(.{3})(?=\1)
Demo 1
or
(.{3,5})(?=\1)
Demo 2
This question already has answers here:
Can regular expressions be used to match nested patterns? [duplicate]
(11 answers)
Closed 6 years ago.
There are multiple C++ files. I need to extract the body of for-loop from these files.
Is there an easy way to do this maybe using grep. Consider there are no nested for loops.
Without parsing the entire file, the answer is no.
for-loops are comprised of a context-free grammar and, as such, cannot be matched by a regular expression.
A more involved approach is to use grep to search for the beginning of a for-loop (for follow by optional whitespace followed by a lpar) then manually find the closing curly.
Unfortunately parsing C++ is Turing Complete, so unless there's some cute flag to pass to your compiler, you're hosed.
This question already has answers here:
Does an algorithm exist which can determine whether one regular language matches any input another regular language matches?
(4 answers)
Closed 9 years ago.
I'd like to take a user-input regular expression and determine whether or not it will match any string, i.e. would it "reduce" to .+ or .*?
I suspect that since this exists, that my question will reduce to the halting problem, but I'd really like to be wrong about that.
I don't think what you want is similar to the Halting problem since the grammar of regular expression. Considering the alphabet and the language recognized by your automaton is finite, you can still use a dummy algorithm that would try every world of your language and test if the regular expression is able to recognize it or not.
In practice, this method has an awful complexity but you don't have any "undefined" state you would have in Halting problem since the number of inputs is enumerable.
I actually don't know if a better version of this dummy algorithm exists, but i hope i answered about your question on similarity to the Halting problem.
This question already has answers here:
How does this regular expression work?
(2 answers)
Closed 7 years ago.
I found a nice piece of regex code that checks for a prime number. I think I understand it but i'm still a little confused. Here's the code: /^1?$|^(11+?)\1+$/
Can someone explain (step by step) exactly what is happening both with the regex code and how it actually relates to knowing if a number is prime or not?
The basic premise is that this regular expression examines a ones representation of the number (e.g. 5 = 11111). By checking for the presence of ones (1) in certain positions or groupings it can identify the number as prime.
Additional References:
Credit where credit is due -
http://montreal.pm.org/tech/neil_kandalgaonkar.shtml
Great explanation - http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/