4 ms·
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
by tabtab 7y ago
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.
*/
- tosh 7y agoThere is a concept of named capturing groups: https://www.regular-expressions.info/named.html https://www.regular-expressions.info/named.html
- earenndil 7y agoLook at perl6 grammars.
- tabtab 7y agoIt 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.
- btilly 7y agoIt sounds like you would like https://en.wikipedia.org/wiki/SNOBOL 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.
- arnarbi 7y agoPCRE has the DEFINE keyword. Here's an example: https://regex101.com/library/gJ7pU0 https://regex101.com/library/gJ7pU0 (not to be confused with named capture groups)
- maddyboo 7y agoI wrote a very crude library like this in VimL to make writing syntax plugins a bit easier: https://github.com/b0o/builder.vim https://github.com/b0o/builder.vim You can see it in use here: https://github.com/b0o/quicktemplate.vim/blob/master/syntax/qtpl.vim https://github.com/b0o/quicktemplate.vim/blob/master/syntax/...
- zmix 7y agohttps://regexper.com https://regexper.com will render your regex as railroad diagram. Maybe a help...
- jnbiche 7y agoHave 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); }