Regular expressions are a text processing system based on a special pattern notation system. Simply put, it provides programmers with the ability t...
For further actions, you may consider blocking this person and/or reporting abuse
Why all that? ChatGPT can do regexes for me! šš
I was recently toying around with making a regex-based Markdown parser and went to ChatGPT for the rescue when the pattern got complex. While I was amazed at how good (and fast!!) ChatGPT can come up with a solution that fits the description...and how well I was able to iterate with ChatGPT to correct some corner cases, I didn't managed to get a perfect regex that match all my use-cases.
I will definitely ask for ChatGPT's help on regex, but the principles are still very fundemental and I should probably invest time to learn it myself š
P.S. Great post @sineni! (and a nice reminder for me to spend time figuring out advanced regex syntax š¢)
so after that **ChatGPT **can do programming for us, we should go home :))))
Can Chat GPT debug, fix errors and do critical thinking?
Yeah he can!
Its a language model ai. It cant think, it can guess very accruately looking based on what it has read, but reasoning is not what it does. Maybe something else will in the future though
How?
I have seen chatGPT do alot of things but not a single instance where it is fixing errors.
Haha I have tried and chatGPT's not very good at coding (no good css), but it's quite good at regexes!
Even if it makes mistakes, you can use it as a faithful assistant, and only slightly correct him. It can easily help create a game on unity, for example. And you can create pictures in midjourney. This greatly simplifies the work.
That's true!
Oh of course! ChatGPT! Hadn't thought of this :) That AI is really good for these snippets.
Believe me, ChatGPT does mistakes very often ;-).
I believe you, I've seen him making up a lot of things!
Really nice article and really well written!
Some feedback, if you dont mind!
I find you mixed the concept Match up a little, calling it match, selection and substring interchangeably. Reads more clear if you are consistent š
Its worth mentioning, IMO, modifiers. They make a great deal to regexes, the ones I use most are multiline, global and case insensitive.
Regarding regex in itself. I have used it extensively, written and debugged it a lot the past 15 years. Now, is it really a useful tool if it is hardly understood by so anyone but the Regex Master. And is it maybe used a little to often if people feel the need to learn this cryptic language?
For me something like email validation makes for a good case but a very limited usecase given that it does in fact, mostly, incur some really quirky character rules.
Syntax validation sure, a lot more specific of a task so fine.
I think that these cases are OK but solutions like regex should be avoided to the longest extent possible since veeery few can read and understand a regex in a reasonable time, much less debug it.
In your real life example above, what you received could instead have been validated something like
This piece of code would not require and regex skills and would be readable by anyone, even someone porting it to another language, if you dont write complete junk code š the win here is code that can be read, discussed, refactored, maintained and stepped through, over compact and portable, which is a long term win I would take any day.
Hope this is not received as harsh criticism since it is my will to share my experience š
Btw, a real life example from me! My colleague spent 4 work days writing a html parsing regex to read markup to render a Netlify component. Day 4 I was dragged, walked him though it and solved it within an hour. He still has no clue how it works but he does not dare to touch it again. Unfortunately there was no other option in that case since netlify required a regex to exexute at all (derp). Thats a bad solution IMO, would have preferred to use document.querySelector API for this admin code snippet š¤·āāļø
Keep writing and posting and thanks for an overall good read!
Love RegExp and been doing them in all sorts of ways since my first foray in Perl (so the comic was especially funny to me). Nice writeup and including the Groups, which where I always get more power.
Single line ones are easy, doing multiple line ones in different languages always shows the nuances in how things work. Great writeup to get the important stuff in one place!
Š”ongratulations š„³! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up š
Super helpful- thanks for sharing!
Thanks for a great article!
Regex is simple and important for string validation.
Thank you for your feedback) This is my first article in English and I was a little worries before publish š
wow, this is for "lazy developers"? i don't wanna know what non-lazy devs would go throught lol.
Btw, i like using regex101.com to create new ones, and i've written down some of the ones i've used over the years, just to have my own mini library that i understand for my regex needs. I recommend you all do that, just to reduce how much googling you gotta do when that need for a regex comes up like once a year. I'm fairly certain most of us don't need regexes more often than that, unless you're in a particular job that happens to need them a lot.
I still don't understand half the stuff you wrote about, but it's a great write up on regex :)
Merci a vous!
Thank you for writing this guide!
I would add a few suggestions:
?a=1&b=c
, first parse it and after that you could check the values in{ a: 1, b: 'c' }
(if needed with a regex).articles/my-id
doarticle/([^/]+)
rather thanarticle/(.*?)(/|$)
. But again, splitting it on/
first (parsing) makes sense here.^
) to prevent ReDos trouble.In short: be very careful when you use regular expressions in production code, they can have a big cost in the areas of:
Cool guide! Although, in your example for search param parsing, there are actually simpler options.
You could split the string by the
&
and again by the=
sign.But there's an even simpler way in JavaScript
I get that you are trying to demonstrate the usefullness of RegExp, but I thought I'd show you this so you know a simpler way to achieve this :)
Just skimming through the article Iām noticing a couple of mistakes:
\s
matches[ \t\r\n\f]
, not just the space character.lock
neither matcheslock
nor4Lock
{3.5}
, which should be{3,5}
(\k)
before, nor do I get it to work with js. Might be good to specify, which language supports which feature.(?:
I find strange. I wouldāve simply said that this is an uncaptured group - not sure what it has to do with logical brackets.Otherwise good write-up. I hope I could be instructive :)
A shame the example was not provided in Perl, since we are talking about Perl Compatible Regular Expressions:
'testdata' =~ /^(?:8|\+7)\d{10}$/
Only in Perl (and Javascript) you can use a regexp without the need of quoting it like a string because it's part of the language.
Another use that I gave it when I was just starting to discover RegExp is to make a bot crawler that looked for information on a book download website, so I could download the books from the command line.
So another use can be Web Scraping.
How would we write the expressions using multiple lines?
Time to shamelessly plug my old package! I had this exact same problem, so I made a little node.js library which helps me write regexes in a more readable way. It's called betteregex and here's a demo:
As you can see, you can write regexes multiline, with spaces and even comments! You can also reuse regexes to write new regexes. The possibilities are endless here!
Do you want to match across multiple lines, match in a multiline string, or format the expression to be written across multiple lines (for readability, for example)?
Actually I'm asking about the Regex string itself. I find long Regex patterns to be awful.
it was really helpful but unfortunately for now
This site has helped me a lot in the past:
regex101.com/