1

I do not understand this point in Larry's book on page 39:

The other point to be careful about is that regular expressions will try to match as early as possible. This even takes precedence over being greedy.

I have this example:

my $ih = 'fred xxxxxxx barney'; 
print $ih =~ /(.*x)/;

It could match fred x which is earlier then red xxxxxxx

but it matches the second possibility, which is greedy and not earliest. Why ?

EDIT

enter image description here

user2925716
  • 949
  • 1
  • 6
  • 13
  • 4
    I think the match is `fred xxxxxxx`. The regex starts at the left, matching the whole line with `.*` first, and will then backtrack to the first encounter of x. See https://regex101.com/r/53Mv4l/1 – The fourth bird Oct 15 '21 at 13:20
  • @Thefourthbird SO why `.*` doesn't do the same but stops at first `\b` ? – user2925716 Oct 15 '21 at 15:05
  • 1
    The `.*` does not stop at the first `\b` It matches until the end of the string. There is an `x` char after it, that has to be matched, and it does. – The fourth bird Oct 15 '21 at 15:07
  • @Thefourthbird PLease see the end of the first paragraph about earliness and greedeness . THere `x*` stops immediatelly. – user2925716 Oct 15 '21 at 15:12
  • 1
    The "early" here does not refer too greedy. It means that the regex **starts** at the left. So the first occurrence of the pattern will match, as it goes from the left to right. See this example, where the first *position* is matched, instead of the first x char https://regex101.com/r/YPnRwI/1 If you select global at the right top, you can see all positions for clarity https://regex101.com/r/ZIES9D/1. – The fourth bird Oct 15 '21 at 15:18
  • 1
    Now it you change the quantifier from `*` to `+` to match at least 1 or more times, you see that the first possibility from the left is the `x` char, and as the quantifier is `+` is will match all consecutive x chars https://regex101.com/r/cgfsWl/1 – The fourth bird Oct 15 '21 at 15:21

0 Answers0