News for hackers. Stuff that matters.
Re:RegExr by tabtab on Monday August 05, 2019 @02:22PM (#20616776)
I find RegEx's a bit cryptic. Sure, with enough practice one can read them, but I'd like an alternative that can easily break out chunks as named units. One can then reference the named units to compose new named units. Divide-and-Conquer.

I'd like to see experiments in alternatives that provide this. It could help do for RegEx's what SQL's WITH statement did for long SQL statements.

Backus–Naur Form could act as a starting point, but it needs some syntactic adjustments to make it more compact and practical, in my opinion. Partial example:

     Side = VarFunc + Segment*;
     Segment = 
         ":" + Varfunc
      | (":" + VarFunc)[0..1] + "{" + "}"
      | (":" + Varfunc)[0..1] + "{" + Base + (";" + Base)* + "}";
     Letter = ('a'..'z'|'A'..'Z');
     ForbiddenChar = $anyChr("/?#%");
     /*
     Legend:
       [x..y] = quantity of repeats (substitute letters with integers)
       [0..n] = zero, one, or multiple
       * = shorthand for [0..n]
       | = "or"
       + = concatenation, ignores white-space
       ++ = concatenation, excludes white space
       +w+ = concatenation, must have white-space between
       'x'..'y' = character falls within range
       $x() = special function or constant, where "x" is a name.
     */
Re:RegExr by btilly on Monday August 05, 2019 @03:23PM (#20617730)
It sounds like you would like https://en.wikipedia.org/wiki/SNOBOL.

Though if you had to do enough work with text, my bet is that you'd eventually figure out why regexes won. They are simple, straightforward, and just enough to tackle the occasional matching problem on your way to doing something else.

Re:RegExr by jnbiche on Monday August 05, 2019 @07:23PM (#20620329)
Have you tried Parsing Expression Grammars (PEGs)? Most implementations allow use to name arbitrary sub-parsers, often using the programming language's native variables.

See for example Peg.js, LPEG (lua), etc. However, PEGs don't natively permit left recursion (note: some PEG implementations do add that on). If that's a problem, Earley parsers are a nice alternative.

Example of a calculator using Peg.js:

  start
    = additive

  additive
    = left:multiplicative "+" right:additive { return left + right; }
    / multiplicative

  multiplicative
    = left:primary "*" right:multiplicative { return left * right; }
    / primary

  primary
    = integer
    / "(" additive:additive ")" { return additive; }

  integer "integer"
    = digits:[0-9]+ { return parseInt(digits.join(""), 10); }
Re:RegExr by maddyboo on Monday August 05, 2019 @05:45PM (#20619380)
I wrote a very crude library like this in VimL to make writing syntax plugins a bit easier: https://github.com/b0o/builder.vim

You can see it in use here: https://github.com/b0o/quicktemplate.vim/blob/master/syntax/...

Re:RegExr by arnarbi on Monday August 05, 2019 @03:23PM (#20617731)
PCRE has the DEFINE keyword. Here's an example: https://regex101.com/library/gJ7pU0

(not to be confused with named capture groups)

Re:RegExr by tosh on Monday August 05, 2019 @02:25PM (#20616817)
There is a concept of named capturing groups:

https://www.regular-expressions.info/named.html

Re:RegExr by zmix on Monday August 05, 2019 @05:56PM (#20619487)
https://regexper.com will render your regex as railroad diagram. Maybe a help...
Re:RegExr by earenndil on Monday August 05, 2019 @03:03PM (#20617431)
Look at perl6 grammars.
Re:RegExr by tabtab on Monday August 05, 2019 @03:09PM (#20617522)
It looks extensive, but perhaps may be overkill for a common regex alternative. Thanks for the tip, though. A standard or semi-standard would have to start simple.