The true power of regular expressions (2012)

(npopov.com)

19 points | by uneven9434 3 hours ago

8 comments

  • lbriner 1 hour ago
    Something that seems obvious but not always implied by people's comments is that people are rarely trying to match an entire document with a regular expression so it doesn't really matter that "HTML is not a regular language".

    If I am trying to e.g. count div tags with a regex like "<div" or whatever, then clearly this would work in 99.9% of cases and probably achieve what the poster is looking for.

    As soon as you also add character classes to ignore various parts of the document that you are not interested in like "<div[^>]*>" or whatever it is, then it is eminently useful even if the bit we are ignoring is not fully regular.

    One lovely thing about regex is how fast it is. I was asked to parse a massive CAN Bus log file for how many times some event had logged. This was the early 2000s and the file was 6GB, which was pretty big. I tried .Net's string.StartsWith or something and that took ages to run through the file. I did the same thing with a regex and it finished in like 5 seconds (HDD, not SSD!). I don't know how the magic works but it is very impressive.

    • Gabriel54 1 minute ago
      Regex can also be horribly slow - it depends on the particular regex you are using.
  • s_dev 1 hour ago
    Just like jq there will be some lad along to tell us that "I don't like the syntax and find it confusing" not realising that's the exact superpower it presents is it's terseness is a key property to it's adoption. jq and regex really are sort of handy one liners that you invoke in other scripts and you explain what they do in your script with a comment.
  • cadamsdotcom 1 hour ago
    Some people, when confronted with a problem, think "I know, I'll get my agent to solve it with regular expressions."

    Now they have three problems.

  • HelloUsername 1 hour ago
    "Doom Using Regular Expressions" https://news.ycombinator.com/item?id=49094081
  • ape4 27 minutes ago
    The last thing we need is huge regexs made by AI
  • evilc00kie 20 minutes ago
    > The regular expression is very simple

    OT but this me-problem makes me angry every time I read it. Nothing is simple, otherwise it is trivial and not worth mentioning. I can't read over this without thinking that I'm not smart enough to wrap my head around something instantly.

  • hn9rsvy2gx 40 minutes ago
    Been burned by this exact thing before
  • ourmandave 1 hour ago
    Every time I cut-n-paste a regex into code, I comment with the url of the spell book page I copied so future me can answer, "WTF does this do again?"
    • bleuarff 23 minutes ago
      I'm wary of external urls in code. Some plaintext comment would come in handy for the day the link inevitably goes dead.