0

i just learned lookahead in regular expression, but there is one detail that is bugging me:

% set test abcdehehfkdehemno

% regexp {([^ ])*.} $test match

1

% set match

abcdehehfkdehemno

% regexp {((?=dehe).)*.} $test match

1

% set match

a

why the ([^ ])* is greedy and the ((?=dehe).)* is not, i expect the lookahead version return m instead a ?

danny
  • 983
  • 2
  • 11
  • 26
  • Are you just asking for the regex explanation? Is it Tcl? What is your real problem? What pattern do you need to match, or what results do you expect per a given input string? – Wiktor Stribiżew Oct 19 '16 at 22:35
  • Both regexes apply greediness. In the second, however, assertion doesn't pass. – revo Oct 19 '16 at 22:36
  • These patterns can be tested at http://regex101.com. The first one just matches 0+ chars other than space and any single char after (thus matching the whole string). The second one requires a substring starting with `dehe`, but as there is no `dehe` at the beginning, and the subpattern can match an empty string, the next `.` in the pattern matches the first `a`. – Wiktor Stribiżew Oct 19 '16 at 22:39
  • why it has to be at the beginning of the string ? there are two `dehe` and i am expecting the look ahead to be greedy and match the second `dehe`. – danny Oct 19 '16 at 23:04
  • @revo but which assertion ? – danny Oct 19 '16 at 23:08

0 Answers0