phpregex

how to interpreter regular expression


I am new to regular expression. in order to study it i am referring to Mastering regular expression.

My regular expression is:

[a-z\s]{3,10}

And the input string is:

cat is the cat

I am checking this on https://regex101.com. in the output i am getting "cat is the " if i change the regular expression to [a-z\s]{1,10} than also i get the same result. so i am unable to get how this output is coming.


Solution

  • Noramlly regex engine would scan for a match in a string from left to right. Here, the actual regex is [a-z\s]{3,10} , it would match 10 chars of lowercase letters or spaces from the start since it was greedy. {3,10} repeats the previous token [a-z\s] min of three times and the max of 10 times. If the line contain lowercase letters or spaces of length 9, this regex would match that line also because 9 falls within the range 3, 10

    [a-z\s]{1,10} also do the same job of matching 10 chars of lowercase letters or spaces from the start. {1,10} would repeat the previous token [a-z\s] minimum 1 time and the maximum of 10 times. So this also matches a line which has single character but the above regex matches a substring which must have at-least three characters.