DEV Community

Discussion on: My RegExp nightmare

Collapse
 
nancyd profile image
Nancy Deschenes

I find them useful and powerful. Maybe I have a superpower...

The way I approach a problem with regular expressions is to break it down into smaller parts, then build on what I have. Let's say I wanted to write a morse code parser. I would probably first try to validate that the input is valid morse code, so I start with a few strings that are valid, and a few that are not. I try my test strings against the regex I think will validate, and see if it works. Then I try replacing the letter E. I make sure that works. Then the letter T... I make sure my "._" doesn't get turned into "ET". Then I build on it...

What I hate about regexs is that they are "write only". That is, if a piece of code has a regular expression in it, you pretty much have to treat it like a black box. If you have to change it to handle a different case (you want to handle digits in your morse code translator) , you more or less have to recreate the regex from scratch. The current version is helpful for hints, but that's about it.

People who can look at a complicated regex and tell you what it does and what cases pass/dont are worthy of great admiration.

Wayne's World.