HACKER Q&A
📣 sergiotapia

Does anyone here truly, know Regex?


I've never been able to learn it truly, and always have to reach for docs and examples.

In fact, in my 15 years of coding I never once met someone who know regex really - they all did the same.

Where is this mythical regex guru and how did you learn it! Please share!


  👤 PaulHoule Accepted Answer ✓
For me proficiency in a language is about knowing how to look up correct answers from the official documentation quickly and not wasting my time looking up wrong answers from stack overflow and programming splogs.

I know most of the common operators in a regex by heart but I do have to look up how to use lookahead or lookbehinds here

https://docs.python.org/3/library/re.html

or

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pa...

because I use them maybe once a year. Another area where people are always getting in trouble with regexes is quoting. That is, you sometimes have to look for a character like '[' which is meaningful in a regex and this can drive you batty when that quoting interacts with other quoting in the language you are using like the quoting in strings or in the bash shell.

If you really want to master regular expressions you ought to understand the theory behind them and how they are implemented,

https://swtch.com/~rsc/regexp/ https://www.cs.princeton.edu/courses/archive/fall12/cos126/l...


👤 runjake
I read the O'Reilly book, Mastering Regular Expressions, back in the late 1990s or whenever it was. And I've been using them heavily ever since.

Most of my surviving colleagues just use some web-based regex constructor of some sort and they get along mostly fine, but would be lost without it.

Asking around, many of them seem fond of:

- https://regex101.com/

- https://www.regular-expressions.info/

- https://learnxinyminutes.com/docs/pcre/


👤 raxxorraxor
I only really learned it by having a problem to solve. You have to be careful with runtime costs, but I often use it to get machine readable info from logfiles.

Also https://regexcrossword.com/ is pretty fun.

Sometimes I just need to grab all public IPs from AWS stuff to whitelist them for something. With Windows powershell for example, although the regex here is crude and would match non-valid IPv4 adresses, it is enough for logfiles:

  $awsIPs = aws ec2 describe-network-interfaces --output=text | Select-String -Pattern ("ASSOCIATION.*\s(([0-9]{1,3}\.){1,3}[0-9]{1,3}$)") | ForEach-Object { $_.Matches.Groups[1].Value } | Select-Object -Unique
Within apps I use them mostly to cut small strings into the respective parts if a simple split doesn't work. I do use them to validate user input regularly, although I don't do user interfaces that often. Not a web developer, but in JavaScript I use them quite often.

I still have to look up some rules from time to time.

For designing the regex I often use sites like this: https://regex101.com/ Here you also get a handy reference and error description.


👤 rthomas6
I use it a bit in Vim find/replace. Nothing crazy. I originally learned it because I made some text file editing programs in Perl, back when that was the thing to use. I don't know a lot, just enough to get by. I don't know if that counts.

For example, if I want to find a function definition for foo in C and I don't know its return type:

    ^\s*\w+\s+foo\s*(.*)
That would find it. Probably.

👤 fargle
Yes, absolutely people truly understand regex. It is a solidly and quite completely explored art of computer science.

This is not an area of learning how to "code" via examples or trial and error. This is an area that requires the study of computer science, aka math. If you find regex confusing, it means that you do not understand, or have not learned about, some fundamental principles of computer science - regular languages, finite state machines, and regular expressions.

This is not intended to be derisive. It's just a very, very common issue - regular expressions are like the algebra and calculus of computer science. It looks like magic if you don't understand it, but it's not magic - it's obvious if you learn the background.

You can code a lot of code without knowing computer science. But you will not learn regular expressions that way.

another +1 for https://regex101.com, btw.


👤 ksherlock
One way to learn would be to implement your own regex engine. (Or at least think about it implementing it.) You don't need to worry about finites and automatons and determinism at this point, a simple recursive descent approach is sufficient for learning.

https://www.cs.princeton.edu/courses/archive/spr09/cos333/be...


👤 tlb
I make simple uses of regexes many times a day, but complex uses only a few times a year. So I know the simple stuff, but have to look at the man page for complex stuff. That seems like the right level of mental burden for a tool.

I would suggest that if you're struggling with complex regexes, they might not be the right tool for the job and you should write a custom parser instead.


👤 themodelplumber
Truly and really are kind of difficult words to draw on to rate my friends' capabilities. But I've seen some in-person examples of regex memory that were pretty mind-blowing at the time. This was back when most every web developer also did server side work or basically DevOps, though.

👤 dundercoder
I always use https://regex101.com when I write regex. The interface is great and provides instant feedback so you can dial in your expressions quickly. But to answer your question directly, no. I don’t really know it.

👤 beardyw
I would take my hat off to anyone who knows all the variations in regex, and what uses them. To me that is the greatest frustration.

👤 billwear
i can do it without looking it up, and i catch my own mistakes on the first go, usually.

want to get there? use emacs for everything, write a lot, and use replace-regex for all search and replace actions.

note that this took several years of being a full-time, open-source doc person 8x7.


👤 DerekBickerton
Obligatory XKCD:

https://xkcd.com/208/

You're right about many people not being fluent in Regex. If you were fluent though, you wouldn't even need to use it that much anyway, so people don't learn it fully. They just Google for patterns that match their criteria.