DEV Community

The beauty of VerbalExpressions

George on August 03, 2017

So at some point in our lives we've all had to encounter the wonders of Regular Expressions (Regex). If you don't know what Regex is in short terms...
Collapse
 
carsso profile image
Germain Carré • Edited

"I don't think anybody likes writing it."
=> I do !

RegEx are life, RegEx are everything !

Collapse
 
caseywebb profile image
Casey Webb

Same. Writing a complex RegEx is like a mini-game.

Collapse
 
pratikaambani profile image
Pratik Ambani

Hey buddy!!!

Can you please suggest me a good tutorial to learn regex??

Collapse
 
rapidnerd profile image
George

Regex has minor different syntax's for every language. Personally I learnt it from this website regular-expressions.info/tutorial....

Thread Thread
 
pratikaambani profile image
Pratik Ambani

I was looking for Java RegEx.

Getting prompt suggestions from experts is the beauty of this site!!
Thanks a lot George.

May code bless you... 🙂

Thread Thread
 
caseywebb profile image
Casey Webb

rubular.com is another great resource. The basics are going to be pretty much the same from language to language.

Collapse
 
rharris2825 profile image
John Harris

I have always turned to regexr.com/ when I have needed help with RegEx.

Collapse
 
erpheus profile image
Guillermo Guridi

Not a tutorial perse, but this puzzle-game is quite a fun practice for them: regexcrossword.com

Collapse
 
meanin profile image
Paweł Ruciński
Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

I don't like this syntax. It seems overly bloated compared to the equivalent regex. Your example also doesn't include grouping, which is vital to extracing information from regex parsed strings. It also doesn't show how counted repitions, like in the regex graphic, would be used.

I'm afraid this would be ridiculously verbose for non-trival expressions.

Collapse
 
rapidnerd profile image
George

Personally I kind of see it per project, how much it'll be used and on the language in general. For some minor usage of it without having to make something overly complicated I find this would be perfect for what is needed. However I do agree with your points, some of my projects (Java and C# specifically) I've stuck with standard Regex due.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

Perhaps there is a happy middle ground. Certainly when I'm doing basic matching I definitely prefer functions like endsWith, contains and startsWith compared to the equivalent regex.

Collapse
 
superboum profile image
Quentin Dufour • Edited

Don't use the email regex given in example, this one is wrong.
Here are some valid email address according to RFC822 that will not match the regex :

  • me@example.museum (the tld contains 6 characters)
  • John Smith <john@smith.com> (Yes, you can put a name in front of an enclosed email address like that)

Some times, using RegEx (or VerbalExpression) is not appropriate. In this case, you should probably write a parser. Here is the implementation of this parser in go : golang.org/src/net/mail/message.go...

Collapse
 
danielbastos11 profile image
Daniel Bastos • Edited

A few months ago, I needed a really complicated Regex for a project that I was working on. After struggling for a very long time with the syntax, I ended up writing a small library to generate the Regex for me. The thing is: I never used it again, so I never got to write a decent documentation.

Using my library, your example would look like that:

import { is, start, end, maybe, not } from 'frogjs';

new Regex(is(
  start, 
  'http', maybe('s'), '://',
  notBefore(' ', maybe('www.'))
  end
));

If you wanna check it out, take a look here: github.com/danielbastos11/frogjs
The test fold is quite complete (I'm surprised, actually).

Collapse
 
rpalo profile image
Ryan Palo

This is awesome! How is this not a billion times more popular than it is?? Thanks for sharing!

Collapse
 
inf3rno profile image
inf3rno • Edited

I think this is a very bad idea, it is much better to learn regex from regular-expressions.info.

I think having a single line complex regex is a code smell. It is a much better approach to build it from sub-patterns or use multiple regex patterns or don't use regex at all. I Have a small example as evidence (note that the code is not tested or not even written in an editor):

Using a single complex pattern (common golden hammer mistake):

function checkIP(address){
    if (!(/^(?:\d|[1-9]\d|1\d{2,2}|2[0-4]\d|25[0-5])(?:\.(?:\d|[1-9]\d|1\d{2,2}|2[0-4]\d|25[0-5])){3,3}$/).test(address))
        throw new InvalidIP();
}

Using multiple small patterns in combination with language features:

function checkIP(address){
    let parts = address.split(".");
    if (parts.length != 4)
        throw new InvalidIP();
    parts.forEach((part){
        if (!(/^\d{1,3}$/).test(part))
            throw new InvalidIP();
        if ((/^0./).test(part))
            throw new InvalidIP();
        let num = parseInt(part);
        if (num>255)
            throw new InvalidIP();
    });
}

Building the pattern from sub-patterns:

function checkIP(address){
    let lessThan256 = "(?:\d|[1-9]\d|1\d{2,2}|2[0-4]\d|25[0-5])";
    let pattern = `^${lessThan256}(?:\.${lessThan256}){3,3}$`;
    if (!(new RegExp(pattern)).test(address))
        throw new InvalidIP();
}
Collapse
 
pildit profile image
pildit

Wow man! you've made my life 100x easier!
Thanks

Collapse
 
robnu profile image
Bruno

Man I freaking hate regex because it makes me feel so dumb. No matter how much I try to learn it, I never seem to really understand it. I hate it lol