Moderator: General Moderators
Can you please advice me on following regular expressions
i understood that it will take either green or blue which is optional but +.+
changes its meaning and it accepts colors which are not green and blue
but i dont understand how is it affecting
i believe backreferences are used here then they shd be using
value in capturing group only , why are these two value accepted -> Added,Queue.
Also if you check the third line, if i dont give any value then that is also accepted
Code: Select all
Code: Select all
var_dump(preg_match('/(green|blue)?+green/', 'green')); // int(0)
.+ works normally: there must be one or more characters. All together the regex is... well, it's unnecessarily complicated. There's three cases of input:
a) The string contains "greenX", which will match with $0=greenX and $1=green
b) Same with blueX: $0=blueX, $1=blue
c) If the string doesn't contain either then it will still be matched because of the .+
Adding the + is basically for performance so don't worry about it too much. Actually adding it is more likely to break a regex than help it because backtracking plays a significant role in how regexes are generally used.
2) Your regex only really matches against the "10:23,Added,". What's happening is that final .* is matching the rest of the line. Try removing it (and the $ with it).
1) For RegEx (green|blue)?+.+
and String value : green
and String value : red
So actually I didnt get your answer or maybe i have not posted my question properly
Thank you so much
"green" matches the first part but since there isn't anything after it does not match the second part. Because of the ?+ the engine will not backtrack to undo the first match (which was optional) so that it can make the second instead. If you change it to just ? then it would backtrack and the string would match.
"red" does not match the optional first part but does match the second part.
You have said that backtracking plays a significant role in how regexes are generally used.
I am not aware of these guidelines...can u guide me any link/site which is a good reference point to understand that
If you want to learn, regular-expressions.info is a good place to start. The documentation for Perl's perlre is another place to get more technical information, and of course there's PHP's own PCRE documentation.