DEV Community

Discussion on: VerbalExpressions - RegularExpression made easy

Collapse
 
slavius profile image
Slavius • Edited

Enyone who tells me Regular Expressions are easy has probably never done anything serious in it. Like matching e-mail address fully based on it's RFC. An example:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
Enter fullscreen mode Exit fullscreen mode

Try to guess what this simple Regex does:

<div>(?:[^<]++|<(?!div>)(?!\/div>))*+<\/div>
Enter fullscreen mode Exit fullscreen mode

Hint: It matches full div tag content that has no other div inside it.

You can read about it here.

Collapse
 
mikeschinkel profile image
Mike Schinkel • Edited

It appears to be missing at least .whitespace(), and the online tool would be much better with auto-complete.

BTW, if anyone struggles to learn common-case RegEx they way I learned RegEx, after thinking I would never be able to learn RegEx, is to use an IDE or Editor that uses RegEx for search and then force yourself to always search by RegEx when applicable.

After doing that I one day realized that I could write working RegEx on the first try, much like how you notice you no longer have a headache.