regexperlnamed-captures

How do I extract and print both these named capture groups in perl?


I want to print both the gclid and the session named captures, but my regex is quitting as soon as it matches the gclid:

echo '"https://example.com/foo/?gclid=abc1234gef76786" session="765dsfsdf7657657khkjh"' | perl -nE '/(?<gclid>gclid=[^&"#\s]*)|(?<session>session=.*)/&&say"gclid: $+{gclid} session: $+{session}"'

Results in:

gclid: gclid=abc1234gef76786 session:

But I want:

gclid: gclid=abc1234gef76786 session: session="765dsfsdf7657657khkjh"

The echo is just an example line from one of millions that will be processed.


Solution

  • That's because you are using the | operator so the regex matching stops when any of the two patterns matches. You can use .* in between the two patterns instead. Put \b before session to ensure word boundary:

    perl -nE '/(?<gclid>gclid=[^&"#\s]*).*(?<session>\bsession=.*)/&&say"gclid: $+{gclid} session: $+{session}"'