0

I have a file containing this single line:

111112111122113

I want to match all two element sequences of 1 in all occurrences.

But when I try this $ perl -lne 'print $1 if /.*(11).*/g' u I get only one result despite the presence of g at the end of my RE:

11

enter image description here How do I print them all, what's wrong with my RExp ?

1 Answer 1

1

Use the non greedy quantifier:

perl -lne 'print $1 if /.*?(11)/g'
#                 here ___^

Demo & explanation

But I think you'd better use a loop:

print $1 while ($inputline =~ /.*?(11)/g)
2
  • $ perl -lne 'print $1 if /.*?(11)/g' u still gives this one result 11 Dec 13, 2021 at 18:48
  • @user1642683: perl -ane 'print $1 while (/.*?(11)/g)' inputfile
    – Toto
    Dec 13, 2021 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.