Russ Cox
swtch.com/~rsc · Personal blog · on HN since 2007
Russ Cox, a Go core developer, writes about regular expression engines, concurrency and the history of Unix tools.
What HN saysHN commenters call his regex series excellent and brilliant, and point to it for explaining linear-time matching and Ken Thompson's NFA approach.
284HN people shared it
220linked it in comments
79stories submitted
1.9Kstory points
4 months agolast shared
2007people per year2026
Most-shared posts
- 1Regular Expression Matching Can Be Simple And Fast149 people · 380 points · first shared 2008 · 33 comments on HN
- 2Implementing Regular Expressions48 people · 102 points · first shared 2010 · 9 comments on HN
- 3How Google Code Search Worked: Regex Matching with a Trigram Index (2012)44 people · 1.0K points · first shared 2012 · 70 comments on HN
- 4Regular Expression Matching: the Virtual Machine Approach26 people · 114 points · first shared 2009 · 2 comments on HN
- 5Bell Labs and CSP Threads22 people · 21 points · first shared 2007 · 1 comments on HN
- 6Regular Expression Matching in the Wild16 people · 116 points · first shared 2010 · 20 comments on HN
- 7Go115 people · first shared 2013
- 8Why threads vs events is a nonsensical question.4 people · 120 points · first shared 2010 · 46 comments on HN
- 9Concurrent Power Series [Squinting at Power Series] [pdf]4 people · 2 points · first shared 2011 · discussion on HN
- 10Homepage4 people · first shared 2010
- 11Newsqueak.pdf2 people · first shared 2013
- 12Toward a new systems programming environment – Russ Cox [pdf]1 people · 24 points · first shared 2021 · 3 comments on HN
- 13Regular Expression Matching Can Be Simple and Fast1 people · 3 points · first shared 2018 · discussion on HN
- 14Regular Expression Matching Can Be Simple and Fast (But Is Slow in Java, etc)1 people · 2 points · first shared 2021 · discussion on HN
- 15Russ Cox: Construction and Maintenance of a Centralized Hash Table1 people · 1 points · first shared 2010 · discussion on HN
What HN people wrote when they linked it
dekdrop · 2026-06-28
I want to share Russ Cox's webpage on regexp https://swtch.com/~rsc/regexp/
I find it a good reading.
rhdunn · 2026-05-25
That goes back to Ken Thompson's NFA regex interpreter from 1968 [1], [2], [3]. Note: that whole regex series by Russ Cox [4] is great.
[1] https://dl.acm.org/doi/10.1145/363347.363387 -- Programming Techniques: Regular expression search algorithm
[2] https://swtch.com/~rsc/regexp/regexp1.html -- Regular Expression Matching Can Be Simple And Fast
[3] https://swtch.com/~rsc/regexp/regexp2.html -- Regular Expression Matching: the Virtual Machine Approach
[4] https://swtch.com/~rsc/regexp/ -- Implementing Regular Expressions
thaliaarchi · 2025-05-15
I find it interesting that MicroPython's `re` module[0] is implemented with a backtracking regular expression engine from re1.5[1], instead of one of the linear-time engines from the same library. (Russ Cox covers the various engines in the excellent blog series[2] which re1 is a companion to.) I figure the choice was made due to binary size or memory constraints, though they're all quite small.
[0]: https://github.com/micropython/micropython/tree/master/lib/re1.5
[1]: https://github.com/pfalcon/re1.5/tree/v0.8.2
[2]: https://swtch.com/~rsc/regexp/regexp2.html
chmaynard · 2023-07-11
Brilliant article. Here's the author's home page: https://swtch.com/~rsc/
knome · 2023-06-21
Russ Cox has an excellent write up on it.
"Regular Expression Matching Can Be Simple And Fast" (2007)
https://swtch.com/~rsc/regexp/regexp1.html
kqr · 2022-11-12
Always a worthy, related mention: https://swtch.com/~rsc/regexp/regexp1.html
Another fun idea is the opposite of the regex: the regex constructs a state machine of the needle and is then quickly able to run many haystacks through it. Sometimes you need the other optimisation: compile the entire haystack down to a state machine, and you can run many needles through it! It becomes a very primitive search index.
bla3 · 2022-11-11
Most don't, but re2 and a few others do. The ones that do use it don't have exponential runtime on malicious inputs and lack a few features (back references, mostly). https://swtch.com/~rsc/regexp/ is a great resource on this.
MisterTea · 2022-03-16
Prior art:
http://man.postnix.pw/plan_9/2/thread (Plan 9's thread(2). Personal note: I absolutely LOVE working with this library)
https://swtch.com/libtask/ (Rus Cox's portable library (Someone on the 9fans mailing list ported it to a micro-controller))
See Also:
https://seh.dev/go-legacy/ (A nice code tour of the historical CSP lineage of Go)
https://swtch.com/~rsc/thread/ (Bell Labs and CSP Threads)
lloeki · 2022-03-05
> Having worked in an industry where the linear vs. exponential performance implications of regular expressions and PCREs was crucial (intrusion protection systems)
same boat. that's why we use re2 in a way inspired by Go (which means losing a few features but the tradeoff is worth it):
> The regexp implementation provided by this package is guaranteed to run in time linear in the size of the input. (This is a property not guaranteed by most open source implementations of regular expressions.) For more information about this property, see
> https://swtch.com/~rsc/regexp/regexp1.html
> or any book about automata theory.
https://pkg.go.dev/regexp
skimdesk · 2022-02-23
Russ Cox has written a great article on comparing regex engine speeds [0]. It taught me a lot about finite automata.
[0] https://swtch.com/~rsc/regexp/regexp1.html
kccqzy · 2021-05-27
Russ Cox's articles on regex engine remain the gold standard: https://swtch.com/~rsc/regexp/
I remember having built my own regex engine (for educational purposes only) following his articles almost a decade ago. Great fun.
Another fun thing you can do (and I have done) is that given a regex, construct strings that fully match the regex. It's not hard at all, but it's quite subtle if you want good results. For example given the regex "a+b+" the code can simply create strings like "ab", "aab", "aaab"... (with one "b" always), but it is slightly more challenging to have it output "aab", "abb", "aabb", etc, which involves forming the Cartesian product of infinite streams in a smart way.
abhgh · 2020-12-21
I agree with this ... I coded a Thompson NFA [1] out of interest a few years ago; definitely recommended as an exercise.
[1] https://swtch.com/~rsc/regexp/regexp1.html
robertelder · 2020-07-28
It's worth pointing out that the resource
https://swtch.com/~rsc/regexp/regexp1.html
linked above is also by the same author, Russ Cox,
as the reference I included at the end of the blog post:
https://swtch.com/~rsc/regexp/regexp2.html
All of the insights that I presented through this visualization tool are based upon the knowledge found in Russ Cox's articles. Also, the link above on RE2:
https://github.com/google/re2
is a project that was was also (started by|heavily contributed to by) Russ Cox. His writings on the topic of regular expressions are absolutely world-class and I have never found any better resource for understanding the deep low-level theory on how they work in practice.
wonnage · 2020-05-01
I personally had the most trouble with regexes because I didn't have a good mental model of how they worked. The hard part wasn't finding the correct symbol/character class I was trying to match, but coming to grips with repetition, greedy/nongreedy, etc.
I took a compilers class in college where one of the projects was to implement a simple regex matcher using NFAs. Bashing my head against this for a week really helped with being able to "read" a regex. Not sure if this was due to finally understanding the algorithm, or the fact that I was just constantly staring at broken regex matches all day.
IMO it was a fairly small time investment for something that is so widely used.
I'll recommend this post that's been on HN many times: https://swtch.com/~rsc/regexp/regexp1.html
kannmig · 2020-03-12
Other great articles on regexes from rsc:
https://swtch.com/~rsc/regexp/regexp1.html
https://swtch.com/~rsc/regexp/regexp2.html
cbarrick · 2019-09-08
"Regular Expression Matching Can Be Simple And Fast" [1] is worth a read (by Russ Cox of Bell Labs and Go fame).
[1]: https://swtch.com/~rsc/regexp/regexp1.html
Tempest1981 · 2019-09-06
Regexps is another area where GoLang used to lag behind Python (for certain types of expressions?). Not sure if things have changed, but this issue is still open 4 years later: https://github.com/golang/go/issues/11646
Not sure I fully understand the NFA vs DFA/re2 choices -- seem like the issue above conflicts with the comment below. Can someone clarify?
From [1]: "Go has chosen to follow the re2 path (not surprising, since Russ Cox is a major author of both Go and re2). re2 has much better performance characteristics than some other regexp engines, in that it never has an exponential slowdown, but that comes at a cost for other regexps (https://swtch.com/~rsc/regexp/)."
[1] https://www.reddit.com/r/golang/comments/84o986/why_is_gos_regex_so_sl…
cfors · 2019-07-02
I wonder what happened with that poor regex expression.
My thoughts are immediately shifting to one of my favorite articles of all time "Regular Expression Matching can be Simple and Fast..." [0]
[0] https://swtch.com/~rsc/regexp/regexp1.html
edwintorok · 2019-07-02
You probably want a regex engine that runs in linear time:
* Google's RE2 https://github.com/google/re2/wiki/WhyRE2
* https://github.com/laurikari/tre/
There is a good series of articles about the problem: https://swtch.com/~rsc/regexp/regexp3.html
I would strongly recommend deploying such a regular expression matcher to avoid problems like this.
There are examples in the above article that you can use to test anything in your production deployment that accepts regular expressions to see how well it copes.
dexen · 2019-05-29
Yes and no. The theoretical "regular expressions" are indeed Type-3 grammars in Chomsky's hierarchy.
In practice, the common "RegEx" implementation implement a lot of extras, that break the theoretical backing, and also exhibit highly non-linear behaviors. Cf. this excellent paper by Russ Cox: https://swtch.com/~rsc/regexp/regexp1.html
mrzasa · 2018-10-30
Not sure what you mean. For detailed complexity analysis, please read this excellent post by Russ Cox: https://swtch.com/~rsc/regexp/regexp1.html
lgessler · 2016-12-27
(Sorry, this is mildly off-topic.) Not sure if this fits your usecase, but you should check out codesearch if you haven't already: https://github.com/google/codesearch
(Russ Cox's excellent writeup is here: https://swtch.com/~rsc/regexp/regexp4.html)
forgotpwtomain · 2016-08-01
I have a re.c file in my best-of collection of code examples locally - but I can't seem to find the original source quickly - nevertheless it was written by Russ Cox[0] and he's spent a lot of time making regular expressions accessible and elegant, his articles are a great resource.
[0] https://swtch.com/~rsc/regexp/
jakozaur · 2016-07-20
Experienced something similar myself. Was even thinking about creating regular expression library which just allow "safe" and fast expression.
The trick would be to not allow only expression that can be translated easily to state automate.
Good regex: "Phone number [0-9]* "
Bad regex: ";Name=.;" as . can also match ";" and it can lead to bad backtracking. You should rewrite this regex to ";Name=[^;];"
RE2 is probably best implementation so far, but because it's tries so hard to preserve backward compatibility with all regular expression it is not that fast in average case:
https://swtch.com/~rsc/regexp/regexp1.html
chubot · 2016-07-20
Admittedly I haven't stepped through all the code, but it's 958 lines and doesn't look horrible. It somewhat resembles the example C programs here:
https://swtch.com/~rsc/regexp/
In particular there don't seem to be crazy optimizations and non-portable stuff like you see in other production regex implementations and in interpreters. It's pure ANSI C.
The functions are all short -- no 1000 line monsters like I've seen in a lot of old C code lately.
The rsc example code is actually a great example of using pointers in C. People rightly avoid lots of raw pointers for application code, but for this use case, they yield compact and elegant code.
twotwotwo · 2016-04-20
Part of the way down the post links to Russ Cox's excellent writeup of how Google used this general sort of technique (turn a regexp into a bunch of conditions involving three-symbol sequences, search an index for them, work from there) to do regexp searching scalably for Code Search: https://swtch.com/~rsc/regexp/regexp4.html
You can peruse his open-source (re)implementation of the approach at https://github.com/google/codesearch .
kyrra · 2015-09-15
I'm not sure the date of the linked page, but this is why RE2[0] was created. Russ Cox has some examples where PCRE falls over flat[1].
EDIT: Looks like the original linked page was first published in 2001[2].
[0] https://github.com/google/re2
[1] https://swtch.com/~rsc/regexp/regexp1.html
[2] https://web.archive.org/web/*/http://perl.plover.com/NPC/
jstimpfle · 2015-06-18
The title is a reference to Russ Cox' excellent article (series) on implementing regular expressions:
http://swtch.com/~rsc/regexp/
wongarsu · 2015-03-05
>How do you extend Boyer-Moore to arbitrary regexps?
As mentioned in the follow-up messages, some regexps can be translated to one or at least few fixed string searches (/^[Gg]rep/ is really a search for "\nGrep" or "\ngrep"). If you can't do that, your regexp may contain longish fixed strings that allow you to discard most lines via Boyer-Moore before you have to run the regexp engine.
For the more general case, http://swtch.com/~rsc/regexp/ provides great links.
SixSigma · 2014-09-03
For more background on this library, read Russ' excellent articles on Regular Expressions
http://swtch.com/~rsc/regexp/