Hacker News new | past | comments | ask | show | jobs | submit login

A use-case for lookarounds that I often use is:

    grep -Po '(?<=...)pattern'
Which also cuts out and prints the relevant part of the line. This saves a trip through cut, awk or perl. (-P is PCRE and -o is print only matched characters, which the lookarounds aren't a part of.)



I often use `\K` instead, which also helps if it is variable length lookbehind

    $ echo 'foo=5, Bar=3; x1=83, y=120' | grep -oP '\b[a-z]+=\K\d+'
    5
    120
further reading: https://stackoverflow.com/questions/11640447/variable-length...


I like to add the lookahead:

  grep -oP '(?<=...)pattern(?=...)'
The caveat however is that the look{ahead,behind} pattern has to be of fixed length.


Wow. I do this all the time, and the additional step of piping to sed never ceased to annoy me.

You've made my day.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: