Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a picture of an NFA, and need to get the resulting regular expression from the NFA. I first had aba*, but realized this doesn't work. I believe it must start with (a|b), but am not sure where to go from there.
There are general approaches on how to translate an NFA to a regular expression, but I do not remember how. However in your case we are helped by the fact that all loops of length more than 1 starts in state 0. So we can use the following regexp;
(b?ab|aab|ab+aa?b)*(ba?|aa|ab+aa?)
Explanation;
(b?ab|aab|ab+aa?b)* is what the regexp would be if state 0 was the only matching state. Since all long loops goes through state 0 this is a prefix of our regexp.
(ba?|aa|ab+aa?) is what our regexp would be if the b links going back to state 0(from state 1 and state 4) where not present. Because we are in state 0 after the previous loop this takes us to an accepting state.
Bringing the two together we have a regexp that looks for traces that goes returns to state 0 an arbitrary number of times and then move from state 0 to an accepting state.
Starting with (a|b) is the wrong approach, because the states 1 and 3 have little in common and how you are allowed to proceed from an a is different from how you are allowed to proceed from a b.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
How to check whether a string only contains certain characters, say "a" and "b" in Scala?
I found several resources online, they are using regex.
For Scala, most of them talked about the findAllIn or findFirstIn function. But I would like to have something that returns true or false.
val s: String = ???
s.forall("ab".contains)
forall checks whether a condition is true for all elements of a collection. So in this case it checks whether a condition is true for each character in s.
contains checks whether a string contains a character, so "ab".contains(x) checks whether x is either 'a' or 'b'.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
what is difference of (b*a)* and (b*a)*? Or they are same language?
The first one is a greedy matching (the asterisk quantifier after the scope) - matches as many characters as possible. The second one is a lazy matching (the asterisk and the question mark) of the same pattern. Lazy means to match as little as possible characters.
Read more here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions
Your greedy expression (the first one) will match the first 4 characters in input bbbac. The lazy algorithm will simply match nothing, because zero length matching is the minimum allowed. Nothing here means an epsilon, and infinite number of epsilons are assumed to exist around every character.
If you translate these patterns to an ABNF grammar you get this:
main = *(*"b" "a")
If you translate it to a language it is:
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to make a regex to validate an amount in which it only accepts numbers, points (.) Optional, 1 comma (,) optional and that after the comma I have at least 2 more numbers, the farthest I've come is this
^(([0-9]{0,})+([.]?))+([,]{0,1}?)([0-9]{1,}?)+$
This works fairly well, the problem is that it allows me to put the period (.) Followed more than once for example (100 ... 000), this accepts it, but I need it to only accept one period (.) At a time, how do i fix it?
I need the regex to validate as follows
100 VALID
100.000,00 VALID
100. INVALID
100..00 INVALID
100, INVALID
100..000,00 INVALID
To prevent the same character (in this case a dot) appearing consecutively, use a negative look ahead anchored to start of input:
^(?!.*[.][.])<rest of regex>
In your case:
^(?!.*[.][.])(([0-9]{0,})+([.]?))+([,]{0,1}?)([0-9]{1,}?)+$
See live demo with test cases from question.
I'm not clear on what you actually want to match, but I don't need to understand that to answer your question, which was how to prevent the same character appearing consecutively.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was thinking on the meaning of side-effect in Clojure. What exactly is a side-effect in Clojure? Could any one explain this with an example?
A side effect in any programming language would be everything that is done which does not have a direct correlation between supplied arguments and the returned result.
(+ 3 4) ; ==> 7 (result is always a mapping between arguments and result. It will always be 7 no matte rhow many times you do it.
(rand-int 4) ; ==> 0,1,2, or 3. You have no idea what it will produce next.
The first expression is functional. You can make a lookup table of all the different two values with it's result and you wouldn't know the difference.
The second might give different result for the same argument. The computation must be based on something else, like internal state, and not the argument alone. It has side effects.
Typical side effects used in programs are I/O and object mutation.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i need some help. My data;
{foreach $page_list as $page}
<li>{$page->name}</li>
{/foreach}
And my pattern;
~\{foreach\s\$(.+)\sas\s\$([^\{]+)\}([^\{]+)\{\/foreach\}~
But it's not working. What is wrong ?
The pattern with which you are trying to exclude the line between the two {foreach...} regions explicitly does not match '{' characters, but two are present in the string. Since that middle area's match is broken somewhere that doesn't get followed by a {/foreach}, the entire pattern fails to match.
That entire portion of the pattern should possibly be .* instead. Rely on backtracking to catch the closing {/foreach}. Of course, this will erroneously match an entire list as one thing, but regular expressions in general can't match quotes (without backreferences, which are technically non-regular and hurt performance), so you might need another parsing strategy entirely.