Why do matches aginst (.*) and (.*?) result in exactly the same result?
Because there's only one ghi.
... /c/ matches "c" (at position 3) /d/ matches "d" /.*/ matches " cd EF ghi jkl" /g/ fails -> backtrack /.*/ matches " cd EF ghi jk" /g/ fails -> backtrack /.*/ matches " cd EF ghi j" /g/ fails -> backtrack ... /.*/ matches " cd EF " /g/ matches "g" /h/ matches "h" /i/ matches "i" -> success
... /c/ matches "c" (at position 3) /d/ matches "d" /.*?/ matches "" /g/ fails -> backtrack /.*?/ matches " " /g/ fails -> backtrack /.*?/ matches " c" /g/ fails -> backtrack ... /.*?/ matches " cd EF " /g/ matches "g" /h/ matches "h" /i/ matches "i" -> success
What needs to be done to get only the "EF" as a result?
(?s:(?!STRING).)* is to STRING as [^CHAR]* is to CHAR.
/cd(?:(?!cd|ghi).)ghi/s
Note: Unlike the trick of adding ^.* to the start of the pattern, this pattern can be used in other patterns.
Note: I think want "EF" instead of " EF ", but it's easier just to trim the whitespaces afterwards.
In reply to Re: Regex problem - (non)greedy?
by ikegami
in thread Regex problem - (non)greedy?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |