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.
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...
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.
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.
Oldest comments (34)
Great post!
Thank you :)
Hope it can solve your problems sometimes.
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.
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.
Yeah, I think you can use the native library of Verbal Expressions at your personal projects. Try out this link
verbalexpressions.github.io/
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.
I would probably still use regex101 at least occasionally. Regex is something that's interesting for me to learn whenever I have the opportunity.
oh I love regex101.com/ (I also use extendsclass.com/regex-tester.html)
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.
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 thatmaybe(' ')
andmaybe(' ')
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.
Pretend there's a tab in that, I can't put one in markdown :) ↩
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!
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.
COBOL disagrees.
... 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!
Try APL before you talk badly about COBOL! ;-)
Nice!
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.
See also: dev.to/rapidnerd/the-beauty-of-ver...
Very nice tool, thanks for sharing
Nice!
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.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.