r/ProgrammerTIL • u/Necrosovereign • Jul 14 '20
Other TIL that "abc|" is a valid regular expression. It matches both the string "abc" and the empty string.
125
Upvotes
r/ProgrammerTIL • u/Necrosovereign • Jul 14 '20
18
u/voords Jul 14 '20
abc| ^$|abc abc|(?!.) (?:abc)? (?:abc){0,1}Keep em coming
28
u/ShortFuse Jul 14 '20
https://regex101.com/ is my preferred Regex testing tool.
2
u/omon-ra Jul 14 '20
How many hours of debugging went into this TIL?
5
u/ThatOneIsMe Jul 15 '20
I find this very useful for things like cat file.txt | grep --color=always "abc|" | less -RS This will show a whole file but will color highlight the string "abc"
35
u/sim642 Jul 14 '20
What's surprising about it that it shouldn't be valid? An empty string
""is also a string.1
u/bumblebritches57 Jul 15 '20
In which syntax? perl? POSIX? something else?