My question is "Do we need regex?".
Some people use regex and said
"It's useful! Short and easy to understand. Only one pattern can show you want to get"
but some people don't use regex because they said
"It's over engineer, Sometime just manipulate string. simple method like
.split
.substring
is enough. And easier to read the logic and understand"
I slightly agree with second opinion because developer know the basic of string method and basic loop/condition.
So what do you all think about it, people?
please share 🙏🏻
Top comments (7)
IMHO, regular expressions are a very powerful tool that should be used only when needed.
Even though It has built in support in many programming languages, it is a language all by itself, and you need to be fluent in that language before you can use it all over the place.
Short, well documented regular expressions that saves you multiple lines of code are great and I do use them from time to time.
The most recent example was when I had to extract some data from a string provided by a 3rd party in the form of "id=value id=value" - the entire regular expression was short and sweet:
"\s17=([+-]?\d+)\s"
On the other hand, long regular expressions are hard to read and maintain and I do prefer to write more lines of code for what I can get from a regular expression like that.
An example for this would have to be the regular expression recommended by Microsoft to validate an email address:
This is something You'll never see in my code. Ever.
I would much rather suffering the few false positives the MailAddress constructor has than have to read that regular expression.
If you're dealing with
.split()
, you're dealing with regular expressions. That isn't an escape.Yes, regular expressions can get big and complex. In my language of choice, you can add comments within your regular expressions. One hand, that's a strong testament to their power and usefulness, but on the other hand, heavy comment on their unreadability.
So, developers should learn to write regular expressions. They should learn to write readable and comprehendible regular expressions. And they should know alternatives that might serve the problem and the next developer (which might be Future Them) better.
I feel myself in matrix movie when I start writing regex :D I feel it complicated and not easy to remember however I tried to study it more than 3 times. \
But let us say a fair word:
but,
Good answer.
I think regex are great.
Mostly, I think they're great for use outside code. With tools like vim, sed and grep.
In code, I think they can be great, but used in the right way. For example, if you want to know if a string starts with something, and your language provides a code
startswith()
function, you should use that.If you want anything more complex, split the regex over multiple lines and comment it. This makes it easier to read, maintain and version-control.
Besides the implementation in codes even you don't want to use it in your implementation.
Regular Expressions are really powerful tools to improve your productivity such as refactoring, some conditional searching, and replacing.
My quick suggestion in implementation is it depends on how complicate of the value you want to replace or matching for instants you want to take some part of the string and capture it to variable
with traditional looping will cause extra logic to extract data from the input and harder to maintain but with Regular Expressions, it would be easier with capturing group like
.*([0-9]{5}).*"([a-zA-Z\s].*)"$
demobut at the end of the day, it depends on the developer who maintains the project to decide this!
Thanks for sharing! I agree with you about using Regex in the command line also. that's right. Another thing about implementation.
Sometimes, when I use Javascript and Java or C#. I use the same regex pattern but the result isn't the same. :*( I hope this is the terrible thing like you mention