Hacker News new | ask | show | jobs
by asicsp 3022 days ago
I came to know about this wonderful site when I saw this article - https://www.rexegg.com/regex-best-trick.html

example:

    $ # all words except those starting with 'c' or 'C'
    $ echo 'Car Bat cod12 Map foo_bar' | grep -ioP '\bc\w+(*SKIP)(*F)|\w+'
    Bat
    Map
    foo_bar
for more details: https://www.rexegg.com/backtracking-control-verbs.html#skipf...
1 comments

Which is a fancy way to say `grep -iv '^c'`. EDIT: Oh, I missed that the input was a single line.

I personally feel that control verbs are bad additions to the regexp, even though I do know that it is not a big addition to the regexp engine itself (e.g. naturally extended from posesssive quantifiers like `a++` or atomic groups `(?>foo)`). Most uses of such verbs can be expressed with combined parsers and simpler regexps, in the much simpler and maintainable way.

sorry, it is not same as `grep -iv '^c'`

the `-o` option allows to output only matching portion, the regex is meant to extract all words other than those starting with 'c' or 'C'

here's hopefully better example

    $ # do something with words not surround by quotes
    $ echo 'I like "mango" and "guava"' | perl -pe 's/"[^"]+"(*SKIP)(*F)|\w+/\U$&/g'
    I LIKE "mango" AND "guava"
Oh, you are right. I missed that all words are in the same line. That said even the original article mentions that it only moves the captured group to the entire match; I am generally in a position to avoid all uses of control verbs, especially if it only costs one or probably two lines of the additional code that I can fully control and comprehend.