304 points · by genericlemon24 · 4y ago · 90 comments
(?(DEFINE)
(?<code>
[A-Z]*H # prefix
\d+ # digits
[a-z]* # suffix
)
(?<multicode>
(?: \( \s* )? # maybe open paren and maybe space
(?&code) # one code
(?: \s* \+ \s* (?&code) )* # maybe followed by other codes, plus-separated
(?: \s* [\):+] )? # maybe space and maybe close paren or colon or plus
)
)
( (?&multicode) ) # code (capture)
( .*? ) # message (capture): everything ...
(?= # ... up to (but excluding) ...
(?&multicode) # ... the next code
(?! [^\w\s] ) # (but not when followed by punctuation)
| $ # ... or the end
) fr”this is both an f- and an r-string”
I had no idea. I wish Python allowed for custom string types. I would love a sql string type if for nothin else than to show my code editor how to highlight inside the string!I didn't know about this and was a happy regular expression user without it, but this looks like a good feature for the specific use case of wanting other people to understand the structure of your regular expressions. And much more portable than I would have expected.
A regex is just a fast, usually integrated into the language, universally understood way to do some simple parsing. Parser combinators are an amazing specialized tool for building parsers, but one that is generally harder to integrate into a code-base (outside of e.g. Haskell), requires lesser known libraries, and is a paradigm that people need time to get used to.
It's like saying people shouldn't use a mitre-box [1] and instead use a full fledged circular mitre-saw [2]. Yes the second tool is much more versatile, powerful and useful. But it requires much more setup, skill and investment to actually use.
[1] https://en.wikipedia.org/wiki/Mitre_box [2] https://www.toolstation.com/power-tools/mitre-saws/c317
It looks like this:
pattern = (
r'[A-Z]*H' # prefix
r'\d+' # digits
r"[a-z']*" # suffix
)
No funky stuff with escapes, and you can indent to your heart's content.https://nim-lang.org/docs/pegs.html
identifier <- [A-Za-z][A-Za-z0-9_]*
charsetchar <- "\\" . / [^\]]
charset <- "[" "^"? (charsetchar ("-" charsetchar)?)+ "]"
Thats a small snippet of how it is used. It's been one of my favourite parts of using Nim to be honest.The fact one can get similar ergonomics this way in straight Python is wonderful! I'm definitely going to leverage this.
I've done similar in other languages, but it's never felt quite right. re.VERBOSE is also handy to know.
Parsing expression grammars are easily one of my favourite tools. Honestly, I find them superior to regexes.
I decided to go looking for when Perl got verbose regex. The oldest thing I can find on perldoc.perl.org or cpan.org is 5.004 (1997), where they were an existing feature.
EDIT: Found 4.036 sources (1993). A quick scan of the man page (troff source!) does not find verbose regular expressions. So it looks like they were introduced very early in the Perl 5 series.
The first definition is as you say: an expression that has a constant value.
The second definition is: an expression that is the primary syntactic form to construct a type. For example: “array literals” construct arrays, but may contain arbitrary expressions within.
The first definition is more common in low-level languages where there is a place in the compiled executable to put constant data. These languages might call the second form an initializer rather than a literal. But in a dynamic language such as Python the distinction is less important.
I suspect a more proper description would be: #1 is the correct definition, formally used for 80 years now. #2 is incorrect, and is being abused by people in JS and Python who should know better.
I would have called them "interpolated strings" or even e-string but the f-string moniker had already caught on and there was no stopping it.
By straight python I mean things like 'for', 'split', 'startswith', 'find', and regular character indexing.
So for me this post is a solution to a problem that I just avoid.
No. f-strings are an awful idea. And combining them with r-strings is yet another awful idea. Please, never do that.
I'm also not a big fan of extended / verbose regular expressions because it creates ambiguity in interpretation (a slightly different language to define regular expressions). It's a bad solution to the problem of building longer expressions, which should've been addressed in a different way: by making the language of regular expressions more modular, not through allowing more hard-to-interpret language details.
why?
― Tim Peters
For example the title is "Star Wars", the subtitle is "The Empire Strikes Back"
[0] https://www.explainxkcd.com/wiki/index.php/1313:_Regex_Golf
So does string formatting. You don't need f-strings for this pattern to work.
even more reason to love python, right now for me slots and dataclasses is my new obsession. there was a great article that was posted here that went into details about 3.8 and up that featured all these great python hacks
https://www.guru99.com/images/1/021020_0523_PythonvsRub1.png
This chart alone should be telling. Ruby has more learning curve, smaller set of libraries and stable but slow rate of innovation. The returns aren't that great for something like Python developers who can be trained on most tool in its ecosystem.
I think the bigger problem is not so much that regexes are generally defined by strings specifying the desired regex, but that at some point between the development of printf and the development of regex libraries people forgot that they could use whatever escape character they wanted when implementing a new conceptual data type. In C, the compiler deals with strings, and the printf function deals with strings, and they try not to conflict with each other by assigning the escape character \ to the compiler while printf uses % instead.
But in Java, and Python, and presumably many, many other languages, some idiot decided that if strings used \ for their escape character, regex functions should also use \ for their escape character. Since they accept strings, suddenly the regex escape character is actually "\\". How do you match a single literal backslash? "\\\\", obviously. What's wrong with that?
I mean python's regex module was added in 1997 and hasn't fundamentally changed since. I don't think that concept was super common in '97.