3 ms·
A recent trap for me: Regex semantics is subtly different across languages. E.g. a{,3} matches between 0 and 3 "a" characters in Python. In JavaScript it match
by ngruhn 1y ago
A recent trap for me:
Regex semantics is subtly different across languages. E.g. a{,3} matches between 0 and 3 "a" characters in Python. In JavaScript it matches the literal string "a{,3}".
- skydhash 1y agoRegex is more a technique than an actual specification. It would be best to find the time to go and read an introductory book about Theory of Computation where they explain the underlying mechanism.
- ryandv 1y ago[flagged]
- skydhash 1y agoIt's half a chapter in most books I know. Or a subset of this 1h MIT videos [0], but the instructor also explains Finite Automata which is the basic mechanism that does all the stuff. [0]: https://www.youtube.com/watch?v=9syvZr-9xwk https://www.youtube.com/watch?v=9syvZr-9xwk
- jraph 1y agoI'll assume sarcasm (from your comment history) but for people actually believing this first degree: good luck debugging an incorrect regex if you haven't practiced regexes. Especially if it was generated by an llm.
- danhau 1y agoI always use regex101 to develop my regexes. It allows you to switch between different engines.
- PhilipRoman 1y agoHonorable mention to [a-z], gotta be my favorite trap
- dataflow 1y agoWhat's the trap for this one? I can't think of any engine that parses this to mean anything other than the letters a through z.
- dpkirchner 1y agoIt depends on its use, ultimately, but if your goal is to find a string of letters (a common use IMO), you'll want to use something like \p{L} to ensure you don't miss non-ASCII characters. eta: fixed regex, I had typed \L, shared from my faulty memory.
- PhilipRoman 1y agoIn some common implementations if $LANG is set to certain values, it will fail to match some ASCII letters. This is because not all latin character using languages put Z last in the alphabet. Try this (you probably need to enable and generate the locale first) echo y | LANG=lv_LV.UTF-8 grep '[a-z]' Locales in general should be considered a "trap", just look at Windows CSV separator handling, etc.
- 1718627440 1y agoNot in general, but using locales for something different than affecting presentation.
- dataflow 1y agoThat's wild. Thanks for explaining. I had no idea this depends on the locale. Looks like I have about a million scripts to fix...
- accoil 1y ago[A-z] though is a fun one though as it includes a few extra symbols between upper and lowercase.
- 1718627440 1y agoDoes it? I thought Regex are defined on character classes not on numeric ASCII values. What would a Regex do on a different encoding then?