Live data from Hacker News

Regular Expressions – Mastering Lookahead and Lookbehind

rexegg.com

21–30 of 85 posts

Re: Regular Expressions – Mastering Lookahead and Lookbehind

#21
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...

Re: Regular Expressions – Mastering Lookahead and Lookbehind

#22

Most of the time I mention the topic of regular expressions to other developers, I usually hear self-critical commentary like "oh, I'm terrible at regex", and rarely anyone who loves them. I think they're great though, if you take the time to understand them. They're something like a Swiss Army knife for programming.

They're definitely useful, and I can cobble them together to get lots of otherwise tedious and complex parsing tasks done, but when I come back to them a week later I have no idea what the hell the pile of wingding vomit I wrote was supposed to do. I find myself writing simpler ones and tying them together with app code just for sanity's sake.

Some regex implementations allow for comments in the string; if your does not, you can probably make it work with concatenation, like:

  String pattern = "^https+" // match the protocol at the beginning
                 + "([a-zA-Z])+" // match the machine name
                 + ...
Honestly, I use regular expressions because, even in such format expanded with comments, I haven't seen anything more readable after you get used to regex operators. I guess the closest would be the alternative format in CL-PPCRE. For instance:

  CL-USER> (cl-ppcre:parse-string "\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b")
  (:SEQUENCE :WORD-BOUNDARY (:GREEDY-REPETITION 1 3 :DIGIT-CLASS) #\.
   (:GREEDY-REPETITION 1 3 :DIGIT-CLASS) #\.
   (:GREEDY-REPETITION 1 3 :DIGIT-CLASS) #\.
   (:GREEDY-REPETITION 1 3 :DIGIT-CLASS) :WORD-BOUNDARY)
But then, any such form can get mouthful:

  CL-USER> (cl-ppcre:parse-string "((\\b[0-9]+)?\\.)?\\b[0-9]+([eE][-+]?[0-9]+)?\\b")
  (:SEQUENCE
   (:GREEDY-REPETITION 0 1
    (:REGISTER
     (:SEQUENCE
      (:GREEDY-REPETITION 0 1
       (:REGISTER
        (:SEQUENCE :WORD-BOUNDARY
         (:GREEDY-REPETITION 1 NIL (:CHAR-CLASS (:RANGE #\0 #\9))))))
      #\.)))
   :WORD-BOUNDARY
   (:GREEDY-REPETITION 1 NIL (:CHAR-CLASS (:RANGE #\0 #\9)))
   (:GREEDY-REPETITION 0 1
    (:REGISTER
     (:SEQUENCE (:CHAR-CLASS #\e #\E)
      (:GREEDY-REPETITION 0 1 (:CHAR-CLASS #\- #\+))
      (:GREEDY-REPETITION 1 NIL (:CHAR-CLASS (:RANGE #\0 #\9))))))
   :WORD-BOUNDARY)

Re: Regular Expressions – Mastering Lookahead and Lookbehind

#23
post #9
post #7

Earlier quoted context omitted.

A programmer saying they are terrible at regex is like a mathematician saying they are terrible at algebra.

Mobile programmers doesn’t need regex nearly as much as serverside

JS programmers don't need to know how a C pointer works, but they're not doing themselves any favors by being ignorant of it. It's very basic basic background knowledge.

Re: Regular Expressions – Mastering Lookahead and Lookbehind

#25
post #21

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...

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.

Re: Regular Expressions – Mastering Lookahead and Lookbehind

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

    grep -Po '(?
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.)

Re: Regular Expressions – Mastering Lookahead and Lookbehind

#27
post #21

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...

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 rege…

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"

Re: Regular Expressions – Mastering Lookahead and Lookbehind

#28
post #26

A use-case for lookarounds that I often use is: grep -Po '(? 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...

Re: Regular Expressions – Mastering Lookahead and Lookbehind

#29
post #8

Earlier quoted context omitted.

I think everyone who doesn't know regex should make learning regex a priority. (However, I find that lookahead and lookbehind in particular do not tend to come in handy very often. So maybe just make a mental note that this exists and then look it up when you need it.) Just learn the basics and maybe take a very quick look at the theory, finite automata (maybe the name puts people off, but its just a couple of circle…

Which version of RegEx? I've "learned" RegEx two or three times and then switched language/platform and had everything I previously learned no longer work reliably. You might think I am just talking about Microsoft's quirky implementation but even in the Linux-sphere it isn't consistent see: http://www.greenend.org.uk/rjk/tech/regexp.html You take a complex format string which was design to use the fewest characters…

That's an overstatement of the differences between various regex engines. They all follow the basic standards, with [] being character classes, () being submatches, * being "0 or more", + being "1 or more", etc.

The two main differences between various engines are which characters are "literal" and which characters are "magic" (Vim's engine is particularly annoying here), and how to write the "convenience character classes" (like what the shorthand for "alphanumeric character class" is). But these are minor issues, once you've learned how to write a regex, these are trivial to look up.

Knowledge of regular expressions transfer from one engine to another just fine.

Re: Regular Expressions – Mastering Lookahead and Lookbehind

#30
post #7

Most of the time I mention the topic of regular expressions to other developers, I usually hear self-critical commentary like "oh, I'm terrible at regex", and rarely anyone who loves them. I think they're great though, if you take the time to understand them. They're something like a Swiss Army knife for programming.

A programmer saying they are terrible at regex is like a mathematician saying they are terrible at algebra.

Regex is useful when you do lots of string processing, like in webdev. Outside of that, I've found uses to be very limited - certainly not worth the upfront time investment. (I mean, sure one can cobble together something that mostly works with a regex testing tool, but you need to either take a college automata course or work through the Friedl in detail to get a basic level of proficiency).
Post reply on HN