3 ms·
Any suggestions to keep regexes simple? They often grow to huge lengths which are hard to read and/or debug. It would be nice to keep modular. Maybe using websi
by x87678r 6y ago
Any suggestions to keep regexes simple? They often grow to huge lengths which are hard to read and/or debug. It would be nice to keep modular. Maybe using websites like this is the only way.
- chubot 6y agoIn Python or Perl, you can use re.VERBOSE or /x to spread your regex across multiple lines. That helps a little, but the syntax is still pretty obscure. I created a new regular language syntax for Oil which looks more like a grammar: Eggex Example: Recognizing Python Integer Literals http://www.oilshell.org/blog/2019/12/22.html http://www.oilshell.org/blog/2019/12/22.html And the nice thing is that you can modularize and compose patterns (without string concatenation). It is more like AST splicing of regexes. There are details in the article but here's how it looks: DecDigit = / [0-9] / BinDigit = / [0-1] / OctDigit = / [0-7] / HexDigit = / [0-9 a-f A-F] / DecInt = / [1-9] ('_'? DecDigit)* | '0'+ ('_'? '0')* / BinInt = / '0' [b B] ('_'? BinDigit)+ / OctInt = / '0' [o O] ('_'? OctDigit)+ / HexInt = / '0' [x X] ('_'? HexDigit)+ / Integer = / DecInt | BinInt | OctInt | HexInt / To accept 123, 0b10101, 0o755, 0xff but reject non-integers.