Kleen regex expression difference

just started to appreciate the regex, and I practice it on regexone.com my question is given an explanation about the wedge "*". I came up with an answer myself "

[ac]* 

but the solution is:

 aa+b*c+ or a*b*c* 

Are there any differences in behavior with the two? especially if i use it with javascript?

Sorry for my bad english.

+6
source share
2 answers

The problem is not well defined since there are no negative examples.

For example, if they ask you in a medical school “what is the name of the device that amputates”, the “car” is technically correct, but probably not what they wanted to hear (because the number of car accidents ends with people with cut limbs). But whether there was a question: "What is the name of the instrument that the medical specialist will use to amputate during surgery," the answer can no longer be a "car."

Similarly, your solution will work for all the cases provided, but it is not as accurate as them. For example, "cba" recognized by your expression, but rejects them (at least not as a match for the entire string; a*b*c* trivially matches "cba" as a 0 length match anywhere in the string, and as a 1-bit match bit "a" ). In this case .* Is also a valid (but completely inaccurate) solution to their problem.

+7
source

There is no difference in the overall regex in JavaScript and in other languages. The task basically requires that you provide the most restrictive regular expression that matches the given patterns. They also provide an alternative answer to show that you can match the pattern with a less restrictive regex. All you need to look for with the provided templates is the following:

  • There are always a -s pairs: aa , aaaa
  • There are 0 or more b -s: b , bbbb , no b
  • At the end there is 1 or more c : cc , c , cc
  • The letters always come in the following order: abc

There are many regular expressions that you can choose to meet these four conditions, so you need to try to provide the most restrictive in order to minimize matches outside of these examples. However, even with the answer provided, you will come across infinitely many other lines.

An even more restrictive regex would be:

 ^(aa)+b*c+$ 

Here the regex requires the line to start with aa and end with c . I assume that the lessons so far have not reached ^ and $ , and therefore the answer provided does not include them.

+1
source

Source: https://habr.com/ru/post/988365/


All Articles