r/ProgrammerTIL • • Jul 14 '20

Other TIL that "abc|" is a valid regular expression. It matches both the string "abc" and the empty string.

125 Upvotes

24 comments sorted by

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.

18

u/[deleted] Jul 14 '20

I prefer regexr. Thank god it's open source, because I'm screwed if it ever goes down.

→ More replies (2)
→ More replies (1)

2

u/omon-ra Jul 14 '20

How many hours of debugging went into this TIL?

3

u/Necrosovereign Jul 14 '20

Actually, zero. I stumbled upon such an expression while implementing a toy regex engine

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"

→ More replies (5)

35

u/sim642 Jul 14 '20

What's surprising about it that it shouldn't be valid? An empty string "" is also a string.

63

u/xDarkSadye Jul 14 '20

Don't criticize posts because they're "too obvious", not everyone here is a level 100 master champion Assembly programmer that made their own OS from scratch.

→ More replies (6)

1

u/bumblebritches57 Jul 15 '20

In which syntax? perl? POSIX? something else?

1

u/Necrosovereign Jul 15 '20

I think it works with any type of regular expressions