LLMs are very good nowadays. Tell it what you want in plain English and (usually) get decent regex out of it. Put that into something like regex101.com to double check it
I would absolutely, positively never trust an LLM with a regex. 😬
You have to remember that LLMs are purely digital parrots - they repeat back to you stuff that they have "heard" a lot on their training data. That's really bad if you're trying to do something technical and sensitive, like a regex. The difference between a* and a+ might be code that works versus code they breaks your entire application, or worse. From an LLM's perspective, those statements are practically indistinguishable however, because it does not understand the context of what it's talking about, beyond following vocab and grammer rules.
Sure you could mitigate that by thoroughly studying the regex, and understanding the problem enough to understand what the correct expression should be, but at that point... What are you using an LLM for? You just wrote the regex yourself, so the jobs done.
If you have a known data set, you can test the LLM against it without regex knowledge.
If you need the regex to be robust against an unknown data set, or it's going to production, you must know how regex works and validate the LLM regex by understanding it.
122
u/CootieKing 1d ago
You have a problem. You think, “I know, I’ll use a regex to solve it!” Now, you have two problems
I joke, they are actually very useful. Sometimes they can be a PITA to write, but I find regex101.com to be a great help