Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Regex..why can’t you just be normal? 🤯
Elias Groll
Elias Groll

Posted on

     

Regex..why can’t you just be normal? 🤯

The problem with regular expressions is that they are too powerful, hence often mistaken for a Parser + Lexer and therefore lead to code which is VERY HARD TO MAINTAIN.

Please devs, when you need to match a c struct - use ANTLR or whatever and avoid what I did back in the day:

/((\w+\s*(,\w+\s*))(\n|\s))?{([^}])}(\n|\s)(\w+\s*(,\s*\w+\s*)*)?;/g 🤪

(ps: it’s only one of many, yes it might have bugs, no I did not find them yet, no it’s not in production anymore..)

Top comments(9)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
lexlohr profile image
Alex Lohr
...former musician, voice actor, martial artist, started coding 38 years ago and turned front-end developer 25+ years ago.

Regex is a simple yet powerful concept, comparable to a hand gun - and as easily abused to shoot oneself in the foot. There are valid uses for it, and a lot more invalid ones. But don't be too harsh on yourself. If all you have is a hammer, everything starts to look like a nail.

CollapseExpand
 
eliasgroll profile image
Elias Groll
Co-Founder of Codesphere & Ex-Googler
  • Location
    Karlsruhe, Germany
  • Work
    Co-Founder at Codesphere Inc.
  • Joined

Agree!

CollapseExpand
 
nickmaris profile image
nickmaris
Former QA Engineer, now SCRUM master at a company with 1200+ employees, father of 2. Feel free to contact me.
  • Location
    Athens, Greece
  • Joined

Terminal and programming languages have some form of regex already, whereas antlr is something you have to install and you still need comments anyway:

github.com/antlr/grammars-v4/blob/...

Regex can be written in multiple lines with comments, clear commit messages and tests.

CollapseExpand
 
eliasgroll profile image
Elias Groll
Co-Founder of Codesphere & Ex-Googler
  • Location
    Karlsruhe, Germany
  • Work
    Co-Founder at Codesphere Inc.
  • Joined

Sure, nothing bad with regex for small things like matching an email, version number or similar.

Just not the best IDEA to treat it as a real parser :)

CollapseExpand
 
codr profile image
Ilya Nevolin
Top Software Engineer x Founder
  • Location
    Following opportunities (currently in Mexico)
  • Education
    Masters, Engineering Degree
  • Work
    Software Engineering Leader
  • Joined

Use tokenizers

CollapseExpand
 
madza profile image
Madza
Discussions. 💬 Tools. 🛠 Resources. 📚 All things productivity. 🎯🚀💯

I always use regexr.com or regex101.com 😉
Based on the rarity I need it, never even consider to learn it fully 😀😀

CollapseExpand
 
eliasgroll profile image
Elias Groll
Co-Founder of Codesphere & Ex-Googler
  • Location
    Karlsruhe, Germany
  • Work
    Co-Founder at Codesphere Inc.
  • Joined

I still use them! It is valuable to be good at regex, especially for one-time tasks where you want to reformat some data :)

CollapseExpand
 
ben profile image
Ben Halpern
A Canadian software developer who thinks he’s funny.
  • Email
  • Location
    NY
  • Education
    Mount Allison University
  • Pronouns
    He/him
  • Work
    Co-founder at Forem
  • Joined

Can you explain ANTLR?

CollapseExpand
 
eliasgroll profile image
Elias Groll
Co-Founder of Codesphere & Ex-Googler
  • Location
    Karlsruhe, Germany
  • Work
    Co-Founder at Codesphere Inc.
  • Joined

"ANother Tool for Language Recognition" - It is a parser generator :)

You can use it in similar ways to regex but it is meant for bigger tasks (like detecting a c-struct) and it can also detect and locate errors.

Every programming language uses a parser to interpret the code or translate it to machine code.

Fun fact: Some parser generators even use regex to create the lexer, which is responsible for splitting the input into tokens.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Co-Founder of Codesphere & Ex-Googler
  • Location
    Karlsruhe, Germany
  • Work
    Co-Founder at Codesphere Inc.
  • Joined

More fromElias Groll

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp