DEV Community

Cover image for VerbalExpressions - RegularExpression made easy

VerbalExpressions - RegularExpression made easy

Felix on August 15, 2018

When you start learning a new programming language, maybe you had been learning follow those steps: variable, assignment, string, operators... On...
Collapse
 
tterb profile image
Brett Stevenson

That seems like a pretty interesting tool!

In the past, when I've run into issues with my regex I usually seek out a regex tester like regex101, but it looks like this might be a more helpful alternative.

Collapse
 
jsn1nj4 profile image
Elliot Derhay

I would probably still use regex101 at least occasionally. Regex is something that's interesting for me to learn whenever I have the opportunity.

Collapse
 
cyr1l profile image
cyr1l
Collapse
 
simonhaisz profile image
simonhaisz

First of all, when I saw the title I was hoping for a reference to bobince's epic html regex parse answer - was not disappointed :)

But more importantly, I think this a great solution for 90% of regex use-cases. In my experience most of these time you have simple patterns like yours where you don't need the full power of regex but there isn't a better alternative in the language. So your end up with PRs where a single character typo will result in an error can easily end up in prod.

Collapse
 
moopet profile image
Ben Sinclair

I don't hate the idea of these, but I do find them awkward.

When I read anythingBut(' ') I wonder, does it mean [^ ] or [^ ]+ or [^ ]*? How do I tell it that I mean any whitespace, like \s instead of a space? How is it clear that maybe(' ') and maybe(' ') are different1 and mean spaces and tabs respectively?

Back references? Discarded groups? All that shenanigans? I think I'd spend longer looking up how to do something with this sort of wrapper than I would just using regex in the first place. You can split regex over lines and add comments for them, so there shouldn't be any ambiguity. You can test them just like you test anything else.

To me, verbal expressions seem like a cut-down wrapper rather than an abstraction, and I'm not sure how they would help in anything but the simplest cases.


  1. Pretend there's a tab in that, I can't put one in markdown :) 

Collapse
 
tux0r profile image
tux0r

every programming language supports Regex for string operations.

COBOL disagrees.

Collapse
 
fc250152 profile image
Nando • Edited

... is COBOL a true programming language? disclaimer: I've used it for more than 40 years, but I think that it's the worst language in the computer world!

Collapse
 
tux0r profile image
tux0r

Try APL before you talk badly about COBOL! ;-)

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
 
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
 
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
 
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
 
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
 
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
 
theoutlander profile image
Nick Karnik

I'm very comfortable with Regex and love it, but I still think this lib is brilliant! What prompted you to start this project? When was it first released?

I have a project on my list to work on where I give it a set of strings and it generates the regular expression automatically.

I think a cool feature for this library would be to be able to reuse a set of existing expressions that others write.

Good work!

Collapse
 
bachnxhedspi profile image
Felix

Oh I am not behind the project; you can find the detail of VerbalExpressions at github.com/VerbalExpressions

I only developed the online tool verbalregex.com which wrapped JSVerbalExpressions for easy writing and testing the VerbalExpressions.

Anyways, can you share some ideas about the algorithm for generating regex from a set of strings??? It sounds like the appliance of some Machine Learning algorithms.

Collapse
 
bachnxhedspi profile image
Felix

I tried to learn but still have to look at the documentation after some months ;(

Thank you, free spacing mode mades it more readable. I will try it next time.

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
 
perigk profile image
Periklis Gkolias

Very nice tool, thanks for sharing

Collapse
 
ben profile image
Ben Halpern

Great post!

Collapse
 
bachnxhedspi profile image
Felix

Thank you :)
Hope it can solve your problems sometimes.

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
 
lalunamel profile image
Cody Sehl

Neat!

Collapse
 
jianfangbh profile image
J. Bladen-Hovell

Nice!

Collapse
 
moopet profile image
Ben Sinclair
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
 
marcelobritowd profile image
Marcelo Brito

Awesome post man, I'm excited to use that in my personal projects. I'm the office I'll try to use the verbalregex.com yet.

Collapse
 
bachnxhedspi profile image
Felix

Yeah, I think you can use the native library of Verbal Expressions at your personal projects. Try out this link
verbalexpressions.github.io/

Collapse
 
tamouse profile image
Tamara Temple

Nice!