News for hackers. Stuff that matters.
Re:Regex visualized as 3D marble run by crazygringo on Sunday October 18, 2026 @10:00AM (#50141164)
Regex is conceptually incredibly easy to understand.

But regex is also syntactically designed by Satan himself.

Most programming languages use commands that are semantically understandable, like "for" and "let" and "return" and mostly symbols we already know like "+" and "=". They use newlines and indentation for organization. And they put multiple commands on multiple lines and you can add comments and everything.

Regex is exclusively punctuation, with its own idiosyncratic meanings, all compressed into a single line, zero organization, zero comments, and impossible to remember what each symbol does if you don't use it daily. Not to mention the nightmare of escaping regexes when expressed as strings, on top of escaping regex symbols within the regex itself.

I swear it's high time we come up with a regex replacement that looks like normal Python code or HTML or something. So instead of:

  ^(\+\d{1,3}-?)?\d{3}-\d{3}-\d{4}$
We would write something like:

  <start>
    <optional>  # Optional country code
      "+"
      <digit>[1-3]
      "-"
    </optional>
    <digit>[3]  # Area code
    "-"
    <digit>[3]
    "-"
    <digit>[4]
  <end>
Same thing. But one is completely inscrutable to someone who hasn't learned regex, while with the other you can guess pretty easily how it works...
Re:Regex visualized as 3D marble run by wongarsu on Sunday October 18, 2026 @10:17AM (#50141308)
If you want readability you can write

    ^(?P<country_code>\+[0-9]{1,3}-?)?[0-9]{3}-[0-9]{3}-[0-9]{4}$
Most regex engines even have a verbose mode (typically the x flag) where this is a valid regex:

  ^
  (   
      \+
      [0-9]{1,3}
      -?
  )?   # optional country code
  [0-9]{3}
  -
  [0-9]{3}
  -
  [0-9]{4}
  $
I am not sure if everyone writes unreadable regex because the reputation of regex makes it seem pointless to try, or if being witty one-liners is a critical part of the value proposition of regex
Re:Regex visualized as 3D marble run by mejutoco on Sunday October 18, 2026 @10:24AM (#50141382)
Regex in xml? Now you have 2 problems. And the comments are not xml comments even.

Ultimately it is a dsl. Should equations in math also use full words and comments?

No, they should not. Maybe a nice syntax for grammars like raku is a better approach.

Re:Regex visualized as 3D marble run by crazygringo on Sunday October 18, 2026 @11:49AM (#50142132)
I couldn't care less if it looks more like XML or Python or C or whatever. It's just an example.

I just care that things have readable text names rather than inscrutable punctuation and single letters.

Yes, it is a DSL. Just like, you know, Python or C or HTML or CSS. Just because it's a DSL doesn't mean it can't actually use keywords instead of punctuation.

Re:Regex visualized as 3D marble run by whattheheckheck on Sunday October 18, 2026 @03:18PM (#50144336)
Actually yes? If math equations also wrote a fluent English way of saying the full thing as well as the literal syntax as well every time it would help more people actually understand it.
Re:Regex visualized as 3D marble run by mejutoco on Sunday October 18, 2026 @05:04PM (#50145465)
If you want text _in addition to_ regex you can use comments in regex, like in https://softwareengineering.stackexchange.com/questions/1783...

> Should equations in math also use full words and comments?

My "also" referred not to have multiple representations but to also in math have prose instead of equations. I could have expressed it better. I did not think of that interpretation.

As for math, I really do not think that is true. Look at how this beautiful proof is so long in prose.

https://mathcs.clarku.edu/~djoyce/java/elements/bookI/propI4...

Is that better than a^2+b^2 = c^2 ?

https://en.wikipedia.org/wiki/Pythagorean_theorem

Notation helps to be concise. Imagine more complicated things without notation in maths. Of course if you look at Principia Matematica we see the limits of notation as well.

Re:Regex visualized as 3D marble run by crazygringo on Sunday October 18, 2026 @07:13PM (#50146462)
The point is, if you don't use regex that frequently, you can completely forget that ^ means start and $ means end. And adding those as line comments ("^ # Start of expression") would be dumb.

That's why a well-designed DSL tries to make its syntax as self-explanatory as possible. Using "start" instead of "^", or "digit" instead of "d".

We're not doing equations here, we're doing engineering. Analogies to proofs are not helpful. There is a very good reason why, in coding, we give descriptive variable names such as "accountBalance" rather than "b".

Conciseness is not a virtue in programming. Clarity is. Things should not be longer than they need to be -- but neither should they be shorter. The name of something should not be 100 characters long, but neither should it be 1 character long. A word or two is generally the sweet spot.

Re:Regex visualized as 3D marble run by mejutoco on Monday October 19, 2026 @05:03PM (#50158023)
> There is a very good reason why, in coding, we give descriptive variable names such as "accountBalance" rather than "b".

In languages with an expressive typesystem you see a lot more single letter variables.

> We're not doing equations here, we're doing engineering

This sentence is nonsense.

> Conciseness is not a virtue in programming. Clarity is

I see what you are saying, but conciseness is also related to clarity, as I showed with the theorem.

Re:Regex visualized as 3D marble run by dmd on Sunday October 18, 2026 @10:58AM (#50141660)
There are certainly alternatives, like https://pomsky-lang.org/
Re:Regex visualized as 3D marble run by bazoom42 on Sunday October 18, 2026 @10:19AM (#50141326)
Most regex dialects allow whitespace and comments, so that already gets you halfway there.
Re:Regex visualized as 3D marble run by dylan604 on Sunday October 18, 2026 @11:01AM (#50141686)
wouldn't it be easier to just add a comment block in the code above the regex?
Re:Regex visualized as 3D marble run by n923263 on Sunday October 18, 2026 @01:31PM (#50143270)
To be fair, that's just begging for the XML in the comment block to get out of sync with the actual regex literal.
Re:Regex visualized as 3D marble run by sgarland on Sunday October 18, 2026 @10:20AM (#50141337)
You can enable extended mode for most engines, which allows you to add white space and comments.

Personally, I like regexes, in the same way that I like shell string manipulation: it looks utterly inscrutable, and performs magic. If I write one in code meant for others, I usually use extended mode with each line commented.

Re:Regex visualized as 3D marble run by ogogmad on Sunday October 18, 2026 @10:30AM (#50141426)
I think some languages do have a library-style version of regex. But the function names aren't consistent between the different libraries. Maybe then we need to put on special goggles to make them look the same.
Re:Regex visualized as 3D marble run by ryangittins on Sunday October 18, 2026 @01:06PM (#50142971)
In practice, including a unit test for any nontrivial regex in your application is very helpful. You can demonstrate which sorts of strings are expected to match and, maybe more importantly, which ones are not expected to match. The same goes for testing the capture group contents.

It may feel silly to have a whole unit test for a one-line regex, but that one line can be really dense. If you wrote a function to parse the string without regex, that function would be complex enough to warrant a unit test, after all.

Introducing regressions when tweaking a regex in your application is also a big risk which unit tests can help mitigate.

Re:Regex visualized as 3D marble run by kbenson on Sunday October 18, 2026 @01:39PM (#50143345)
What you want is something akin to a Raku (perl6) grammar, which was their enhancement to matching they developed to go beyond regular expressions.

    grammar IniFile {
        token key   { \w+ }
        token value { <-[\n;]>+ }
        token pair  { <key> \h* '=' \h* <value> \n+ }
        token TOP   { <pair>+ }
    }

    my $text = "user = alice\nrole = admin\n";
    my $match = IniFile.parse($text);

    if $match {
        say "Parsed successfully!";
    }
There are various libraries for other languages that try to accomplish something similar or Port the concept, so if this interests you, maybe you can find one. Or in this day and age, maybe you can have some VC (and much of society) subsidize the fairly quick creation of a library for you language of choice through AI.

If you're not looking for that, and more specifically what you have shown above, there are often ways to accomplish that in certain languages. In Perl it's easy because you can use regex cars in a regex, so you can just assign `my $digit = qr/(? :0-9)/;` and then use it in the regex as you did (and Perl already supports non matching whitespace with a flag). For that matter, Perl regexes allow you to define chunks purely within the regex to be reused in a DEFINE block, so possibly that is a feature other regex engines have as well.

Re:Regex visualized as 3D marble run by lizmat on Sunday October 18, 2026 @03:13PM (#50144292)
If you're using Python, you may be in luck: https://andrewshitov.com/2026/10/03/using-raku-in-python/ which specifically shows using Raku grammars / action in Python.
Re:Regex visualized as 3D marble run by yoav_lavi on Sunday October 18, 2026 @04:45PM (#50145311)
Melody (https://github.com/yoav-lavi/melody) is very similar in syntax to your example.

For example:

  <start>;
  
  option of "v";
  
  capture major {
    some of <digit>;
  }
  
  ".";
  
  capture minor {
    some of <digit>;
  }
  
  ".";
  
  capture patch {
    some of <digit>;
  }

  <end>;
Matches semver tags, e.g. v1.0.0
Re:Regex visualized as 3D marble run by nmz on Monday October 19, 2026 @02:39AM (#50149104)
FWIW, lpeg is nice, of course you'll still have to deal with the terseness, but I don't think there's anything that can be done, since you will have to write an expression.

  patt <- country area "-" phone fin
  country <- ("+" %digit^-3 "-")?
  area <- %digit^3
  phone <- %digit^3 "-" %digit^4
  fin <- ! .
Not that impressive, but at least it doesn't group captures and groups and string escapes \ with a class labeling symbol.
Re:Regex visualized as 3D marble run by nagaiaida on Tuesday October 20, 2026 @12:34AM (#50161387)
this is basically how raku fixed perl regex