DEV Community

Cover image for VerbalExpressions - RegularExpression made easy
Felix
Felix

Posted on

VerbalExpressions - RegularExpression made easy

When you start learning a new programming language, maybe you had been learning follow those steps: variable, assignment, string, operators... One major theme you need to focus is string operations. Fox example: get first name from fullname, find and censor all mobile numbers in message,...

Along the operation we usually need to process some common procedure. One repeated procedure is finding a substring and implement some operations over the substring. Maybe you had done like something like this in the very beginning of your learning path.

int checkMatchStubPattern(char* string) {
    for(int i = 0; i < strlen(string); i++) {
        // logic for checking string pattern
        ...
    }
    return ...
}

Not a wrong way, but a time consuming. You must change the checking logic in every case. More code, more bug and of course hard for maintainance. Luckily, Regular Expression - Regex come as a hero to solve those kind of problems: find, input validation... As a confirmation for the usage of Regex, every programming language supports Regex for string operations.

Regular expression is a sequence of characters that define a search pattern. This pattern is then used by string searching algorithms for "find" or "find and replace" operations on strings, or for input validation. (Source: Wikipedia)

Regex is an efficient tool to solve that problem; but it comes at a price. It is really hard to read and understand (but not hard to learn). First try to read the example below.

Check whether a string is a valid url or not

Because of complicated syntax, it is very hard to read and understand Regex. Furthermore, you seems not to work with Regex too often. ROI (return-on-investment) is too low; almost common Regex you need to use can be found on the internet (password, url, IP address,...). Are you willing to spend some weeks for learning something that you only use 4 or 5 times a year? Or just skimming over some sites for the result in around 5 minutes? That way of thinking make developers tend to google some Regex and modify to fit into their needs. Sometime it can cost some hours to a whole day for the repetition process of searching - modifying...

In every angel a demon hides...

Regex solve the string operation problem; but how about the problem of Regex? Fortunately, it can be solved with Verbal Expressions. Try to look at this example.

VerEx()
.startOfLine()
.then('http')
.maybe('s')
.then('://')
.anythingBut(' ')
.endOfLine();

I hope that you will not be frustrated after reading this example. The Verbal Expressions above is defined following this rule:

  • The URL must start with either "http" or "https".
  • The URL must then have "://".
  • The URL can have anything following "://", as long as is not a space.

The generated Regex from the above code is: /^(?:http)(?:s)?(?:\:\/\/)(?:[^ ]*)$/. A bit diffenret but the functional is the same. You can find the implementation of Verbal Expressions in several languages here.

VerbalExpressions solve the biggest probelem of Regex. It is readable and easy-to-understand regular expressions. In my opinion, the transition from Regex to VerbalExpressions is great as the movement from SQL to ORM.

Anyways, VerbalExpressions still have some drawbacks. You need to install a new library to your project, sometimes it is quite painful (e.g. you client, manager... don't think it's neccessary). In that case, you can go to VerbalRegex; write the code and it will generate the Regex for you.

Online tool for generating Regex

Try this tool by accessing verbalregex.com.

Conclusion

VerbalExpressions is not a replacement of Regex; but an easy way to write readable Regex. It can ease the pain of Regex, and actually make writing expressions fun again. But keep in mind that Regex still seems to be the best choice in some complicated cases.

Latest comments (34)

Collapse
 
vdedodev profile image
Vincent Dedo

I've used verbal expressions a bit before and didn't find it to be that useful, it doesn't make regex that much easier to read and it's just another dependency on your project.

Collapse
 
hritik14 profile image
Hritik Vijay • Edited

While this seems a very nice readable library, I doubt the usability. It requires the user to know beforehand what functions she can use (maybe(), anythingBut() ...) If the user already knows what she can do, it's not too hard to do the same in regex.

Collapse
 
bachnxhedspi profile image
Felix

You're right, but it's only take around some minutes for reading the API and in my case it is easier than reading Regex documentation.

And one more advantages of VerbalExpressions is making the code more readable.

Collapse
 
muehan profile image
André

thanks for sharing!! this will help me one day.
If you want to learn real Regex with fun, try this:
regexcrossword.com/

Collapse
 
bachnxhedspi profile image
Felix

Thank you, what an interesting game :)

Collapse
 
benccwai profile image
benccwai

Thanks for posting a great tool,it brings too much convenience to my life but why do you say so :"the transition from Regex to VerbalExpressions is great as the movement from SQL to ORM."

I am thinking the regex can be applied for the SQL as well

Collapse
 
bachnxhedspi profile image
Felix

Using ORM instead of SQL is more easier; It like writing VerbalExpressions instead of Regex.

Overall, I only mentioned about the convenience :)

Collapse
 
lalunamel profile image
Cody Sehl

Neat!

Collapse
 
malgosiastp profile image
Malgosia

I like this tool.

But I think I would rather use it as a helper to learn the regex, not the replacement. Especially at the beginning when you can start writing expression with VerbalExpressions and see the result in regex after that :)

Collapse
 
hoelzro profile image
Rob Hoelz

This is really neat! One place I would really like to see this is with regular expression libraries that don't support extended mode like Perl's regular expressions do - in particular, I'd like to see a Vimscript implementation. One, because of that lack of extended mode support, and two, because Vim's pattern language is pretty different from POSIX/Perl's regular expression language. I dream of the day where I can rewrite this:

  syn match perlVarPlain "\%([@$]\|\$#\)\$*\%(\I\i*\)\=\%(\%(::\|'\)\I\i*\)*\%(::\|\i\@<=\)" nextgroup=perlVarMember,perlVarSimpleMember,perlPostDeref

...into something immediately understandable.

Collapse
 
mt3o profile image
mt3o

If you don't know how to use regular expressions, then just don't use them. Ask someone to help you. Libs like this one help understand how do the regexps work, but for production use - use "regular" regular expressions. They perform better (real, effective code is faster than created with vreg), and more people know regexp than this. If have to - perhaps extract regexps to separate file and apply proper dovumentation. You can use multiline regexps and add comments, you know? And load them from separate file, with all support your IDE can provide. Check the freespacing mode, Michael Kohl mentioned.

Collapse
 
jeremy profile image
Jeremy Schuurmans • Edited

Really like this post. I’m a student and I’ve talked to quite a few others who work through some Regexp exercises and then hope they won’t have to use it ever again. I could see this as a nice tool for getting people comfortable with the logic behind regular expressions.

Collapse
 
tamouse profile image
Tamara Temple

Nice!

Collapse
 
perigk profile image
Periklis Gkolias

Very nice tool, thanks for sharing

Collapse
 
moopet profile image
Ben Sinclair
Collapse
 
lobsterpants66 profile image
Chris Shepherd

Personally I have always found regex to be a write-only language and this is unlikely to change as I only use them once in a blue moon.

So this is a great post as I had not come across verbal expressions before. They look really useful and another tool I can use to make my code easier to understand. Must have a play next time i reach for a regex.

Collapse
 
jianfangbh profile image
J. Bladen-Hovell

Nice!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.