Click here to Skip to main content
15,887,477 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I love not using regular expressions PinPopular
trønderen13-Dec-23 10:06
trønderen13-Dec-23 10:06 
GeneralRe: I love not using regular expressions Pin
honey the codewitch13-Dec-23 14:29
mvahoney the codewitch13-Dec-23 14:29 
GeneralRe: I love not using regular expressions Pin
Nelek13-Dec-23 20:42
protectorNelek13-Dec-23 20:42 
GeneralRe: I love not using regular expressions Pin
pivertt13-Dec-23 21:01
pivertt13-Dec-23 21:01 
GeneralRe: I love not using regular expressions Pin
Paul Sanders (the other one)13-Dec-23 22:15
Paul Sanders (the other one)13-Dec-23 22:15 
GeneralRe: I love not using regular expressions Pin
thewazz14-Dec-23 2:25
professionalthewazz14-Dec-23 2:25 
GeneralRe: I love not using regular expressions Pin
Paul Sanders (the other one)14-Dec-23 4:12
Paul Sanders (the other one)14-Dec-23 4:12 
GeneralRe: I love not using regular expressions Pin
Member 1330167915-Dec-23 19:40
Member 1330167915-Dec-23 19:40 
Quote:
I don't understand why it's difficult - DFA at least.


I am going to answer your question as thoroughly as possible. Regexes are simple in theory, not in practice, which is why people have problems with them.

=========================================

In theory, that's all you need to know. In practice, that's just the start - you missed '^' (which has two meanings), '\' (used for escaping those special characters) and '$'.

In $PROGRAM, which of the special characters need to be escaped? How about the replacement expression in "%s" (different to the match expression "/s")? How does it match newlines (hint: in Vim, for example, '$' vs '\n' vs '\r do all different things).

Write your expression to work in vim, and it fails in your Javascript program. Write your expression in sed and it fails using the regex library in C#. The expression that works in the default invocation of grep fails in the default invocation of Perl. Use `grep -E` and the expression fails on some tools but not on others.

Even passing a regex on to an engine is difficult: in an interactive bash shell you'd use sed "s/\\t//g". In a script that sets the results of that invocation to an environment variable you'd use sed "s/\\\\t//g". You run into a similar problems within your programs when you pass around string variables containing regexes, which is why even though many of the programs which use match expressions in their configuration (like nginx) have quoting and escaping rules that differ to the command-line programs which use the same regex library.

When you use regex liberally in Python, Bash, Grep, Vim, C#, Perl, Javascript and everything else, you never remember how they all handle the special cases - you have to keep looking them up for that particular program.

I'm fairly comfortable with them, having spent the 90s as a Perl programmer, and having used Vim as my default coding editor daily for almost 30 years during which time I collected a couple of postgraduate CS (not IT) degrees (which means I know automata theory better than most), and yet even I have to look regex stuff up on a per-product basis. I am skeptical that you can look at an expression and go "This will work in $x, $y and $z, but not in $a, $b and $c.", and if my skepticism is correct, then you have problems too, but just don't know it.


And that is why people have problems with them - you never quite know which contexts allow '.' to be used to match a period and which ones use '.' to mean "any character", which character classes in a "[]" is allowed, how many levels of escaping needs to be performed, which of the special characters should be escaped and which should not.
GeneralRe: I love not using regular expressions Pin
jmaida14-Dec-23 15:49
jmaida14-Dec-23 15:49 
GeneralRe: I love regular expressions Pin
jmaida13-Dec-23 10:37
jmaida13-Dec-23 10:37 
GeneralRe: I love regular expressions Pin
Mike Hankey13-Dec-23 12:43
mveMike Hankey13-Dec-23 12:43 
GeneralRe: I love regular expressions Pin
jmaida13-Dec-23 14:44
jmaida13-Dec-23 14:44 
JokeRe: I love regular expressions Pin
Daniel Pfeffer13-Dec-23 21:14
professionalDaniel Pfeffer13-Dec-23 21:14 
GeneralRe: I love regular expressions Pin
Matt Bond14-Dec-23 4:02
Matt Bond14-Dec-23 4:02 
GeneralRe: I love regular expressions Pin
MarkTJohnson14-Dec-23 4:39
professionalMarkTJohnson14-Dec-23 4:39 
GeneralRe: I love regular expressions Pin
k505414-Dec-23 5:01
mvek505414-Dec-23 5:01 
GeneralRe: I love regular expressions Pin
MarkTJohnson15-Dec-23 7:31
professionalMarkTJohnson15-Dec-23 7:31 
GeneralRe: I love regular expressions Pin
bryanren14-Dec-23 5:24
bryanren14-Dec-23 5:24 
GeneralRe: I love regular expressions - natural language Pin
jmaida13-Dec-23 15:31
jmaida13-Dec-23 15:31 
GeneralRe: I love regular expressions - natural language Pin
TNCaver15-Dec-23 10:29
TNCaver15-Dec-23 10:29 
GeneralRe: I love regular expressions Pin
GuyThiebaut13-Dec-23 19:36
professionalGuyThiebaut13-Dec-23 19:36 
GeneralRe: I love regular expressions Pin
jmaida14-Dec-23 15:30
jmaida14-Dec-23 15:30 
GeneralRe: I love regular expressions Pin
GuyThiebaut14-Dec-23 20:08
professionalGuyThiebaut14-Dec-23 20:08 
GeneralRe: I love regular expressions Pin
honey the codewitch14-Dec-23 4:12
mvahoney the codewitch14-Dec-23 4:12 
GeneralRe: I love regular expressions Pin
honey the codewitch14-Dec-23 4:14
mvahoney the codewitch14-Dec-23 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.