Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference with Kleen regex expression

just started to appreciate regex and I am practicing it on regexone.com my question is given the explanation about kleene "*". I came up with an answer on my own"

[a-c]*

but the solution is:

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

is there any differences in terms of behavior with the two? especially if I use it with javascript?

sorry for my bad english.

like image 390
loki9 Avatar asked Jun 02 '15 05:06

loki9


People also ask

What is the difference between * and * in regular expression?

In regular expression syntax . represents any single character (usually excluding the newline character), while * is a quantifier meaning zero or more of the preceding regex atom (character or group).

What is the difference between regular expressions and pattern matching?

Pattern matching is used by the shell commands such as the ls command, whereas regular expressions are used to search for strings of text in a file by using commands, such as the grep command. The following table lists some of the equivalent regular expressions for corresponding pattern matching characters: Table 1.

What does the plus symbol mean in a regular expression?

The Plus symbol (+): It tells the computer to repeat the preceding character (or set of characters) for atleast one or more times (upto infinite). Example : The regular expression ab+c will give abc, abbc, abbc, … and so on. The curly braces {…}:

What are regular expressions in Python?

Regular expressions allow you to select specific strings from a set of character strings. The use of regular expressions is generally associated with text processing. Regular expressions can represent a wide variety of possible strings.


2 Answers

The problem is insufficiently defined, as there are no negative examples.

For example, if they ask you in medical school "what is the name of the device that amputates", "a car" is technically correct, but probably not what they wanted to hear (as a number of car accidents end up with people with cut off limbs). But had the question been "What is the name of the instrument a medical professional would use to perform an amputation during surgery", the answer can't be "a car" any more.

Similarly, your solution will work for all provided cases, but it is not as precise as theirs. For example, "cba" is recognised by your expression, but is rejected by theirs (at least not as a match of the whole string; a*b*c* trivially matches "cba" as a 0-length match anywhere in the string, and as a 1-length match of the "a" bit). For that matter, .* is also a valid (but completely imprecise) solution to their problem.

like image 136
Amadan Avatar answered Oct 03 '22 09:10

Amadan


There is no difference in the general regex behavior in JavaScript and in other languages. The task basically requires that you give the most restrictive regex that matches the patterns provided. They also provide an alternative answer to show that you can match the pattern with a less restrictive regex as well. The things to look for with the provided patterns are the following:

  1. There are always pairs of a-s: aa, aaaa
  2. There is 0 or more b-s: b, bbbb, no b
  3. There is 1 or more c at the end: cc, c, cc
  4. The letters always come in this order: abc

There is a lot of regex you can come up in order to match these four conditions so you would need to attempt providing the most restrictive one in order to minimize the matches outside of these examples. Still even with the provided answer you will match infinitely many other strings.

An even more restrictive regex would be:

^(aa)+b*c+$

Here the regex requires that the string starts with aa and ends with a c. I assume that the lessons still haven't gotten to ^ and $ and thus the answer provided does not include these.

like image 44
Konstantin Dinev Avatar answered Oct 03 '22 09:10

Konstantin Dinev