Live data from Hacker News

Show HN: I wrote a book on Python regular expressions

news.ycombinator.com

31–40 of 51 posts

Re: Show HN: I wrote a book on Python regular expressions

#31
post #7

You might like: https://github.com/mtrencseni/rxe So you can write: username = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '%', '+', '-'])) domain = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '-'])) tld = rxe.at_least_at_most(2, 6, rxe.set([rxe.range('a', 'z'), rxe.range('A', 'Z')])) email = (rxe .exactly(username) .literal('@') .exactly(domain) .literal('.') .exactly(tld) )

This seems cool, but I think that's mostly because the regex you think of as a point of comparison is something like r'^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,6}$' which does look awful. But there's no reason you can't do username = r'[\w.%+-]+' domain = r'[\w.-]+' tld = r'[a-zA-Z]{2,6}' email = username + r'@' + domain + r'\.' + tld which is arguably easier to read than the rxe version for someone familiar with regex.

wow I didn't know you could concatenate regexes, thanks

Re: Show HN: I wrote a book on Python regular expressions

#32

Earlier quoted context omitted.

This seems cool, but I think that's mostly because the regex you think of as a point of comparison is something like r'^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,6}$' which does look awful. But there's no reason you can't do username = r'[\w.%+-]+' domain = r'[\w.-]+' tld = r'[a-zA-Z]{2,6}' email = username + r'@' + domain + r'\.' + tld which is arguably easier to read than the rxe version for someone familiar with regex.

wow I didn't know you could concatenate regexes, thanks

Why not? They're just strings until they're compiled. "Code is data"

Re: Show HN: I wrote a book on Python regular expressions

#33
post #7

You might like: https://github.com/mtrencseni/rxe So you can write: username = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '%', '+', '-'])) domain = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '-'])) tld = rxe.at_least_at_most(2, 6, rxe.set([rxe.range('a', 'z'), rxe.range('A', 'Z')])) email = (rxe .exactly(username) .literal('@') .exactly(domain) .literal('.') .exactly(tld) )

This seems cool, but I think that's mostly because the regex you think of as a point of comparison is something like r'^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,6}$' which does look awful. But there's no reason you can't do username = r'[\w.%+-]+' domain = r'[\w.-]+' tld = r'[a-zA-Z]{2,6}' email = username + r'@' + domain + r'\.' + tld which is arguably easier to read than the rxe version for someone familiar with regex.

Yes, an rxe is longer. But: I've been using regexps was 20 years and I can't remember them (both reading and writing). My brain swaps it out, partly bc I know it's one SO away. But that's bad for code readability.

Re: Show HN: I wrote a book on Python regular expressions

#34

Earlier quoted context omitted.

wow I didn't know you could concatenate regexes, thanks

Why not? They're just strings until they're compiled. "Code is data"

The thought of doing this literally didn’t occur to me... I think this is great!

Re: Show HN: I wrote a book on Python regular expressions

#35
post #14
post #7

You might like: https://github.com/mtrencseni/rxe So you can write: username = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '%', '+', '-'])) domain = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '-'])) tld = rxe.at_least_at_most(2, 6, rxe.set([rxe.range('a', 'z'), rxe.range('A', 'Z')])) email = (rxe .exactly(username) .literal('@') .exactly(domain) .literal('.') .exactly(tld) )

10 lines to replace maybe 20 characters. How horribly verbose.

That describes all of Python, to an APL programmer.

Re: Show HN: I wrote a book on Python regular expressions

#36
post #27

There two kinds of text search/parse problems: - The really easy ones. A simple string search/split will do and a regex would be overkill. - The really hard ones. You'll need to fully parse this and using a regex will result in fragile/hard to understand code. Please don't use regexes in production software. Learn how to write simple parsing code.

Can you elaborate a bit on this please? I'd be interested in resources on writing better parsers! Quickly looking at the python standard lib (urlparse, shlex, etc) and Python packages (NLTK Treebank tokenizer), a lot of packages related to slicing, dicing and parsing strings use a mashup of regex and rule based code.

Parser combinators are the way to go. I don't use python often enough to know if such a library exists for python, but I would assume so.

Re: Show HN: I wrote a book on Python regular expressions

#37

There two kinds of text search/parse problems: - The really easy ones. A simple string search/split will do and a regex would be overkill. - The really hard ones. You'll need to fully parse this and using a regex will result in fragile/hard to understand code. Please don't use regexes in production software. Learn how to write simple parsing code.

I couldn't agree more. I honestly find it rather baffling that someone would write a book on not just regular expressions in general, but one particular variant in one particular language.

I've been writing code for over 15 years and if I said I had reached for regular expressions maybe 10 times in that entire time, I'm pretty sure I wouldn't be off even by an order of magnitude.

There are very few cases where they're the appropriate tool, I think. They don't compose(unless you use a higher level library that lets you construct them declaratively maybe), they are hard to read and hard to debug and nearly impossible to extend.

In my opinion, parser combinators are state of the art for building parsers. They can read like prose, are very easy to extend and usually easy to debug as well since you can easily get detailed error messages.

Re: Show HN: I wrote a book on Python regular expressions

#38
post #7

You might like: https://github.com/mtrencseni/rxe So you can write: username = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '%', '+', '-'])) domain = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '-'])) tld = rxe.at_least_at_most(2, 6, rxe.set([rxe.range('a', 'z'), rxe.range('A', 'Z')])) email = (rxe .exactly(username) .literal('@') .exactly(domain) .literal('.') .exactly(tld) )

This seems cool, but I think that's mostly because the regex you think of as a point of comparison is something like r'^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,6}$' which does look awful. But there's no reason you can't do username = r'[\w.%+-]+' domain = r'[\w.-]+' tld = r'[a-zA-Z]{2,6}' email = username + r'@' + domain + r'\.' + tld which is arguably easier to read than the rxe version for someone familiar with regex.

And/or use verbose mode -- re.VERBOSE or (?x) --for comments and whitespace.

Re: Show HN: I wrote a book on Python regular expressions

#39

Earlier quoted context omitted.

None that I know of. My thought process has always been that regex was language agnostic. I think Perl has its own version that is widely used. But other than that, I do not know.

> My thought process has always been that regex was language agnostic. +1 I was surprised to find that regex in python was not much different from the language I use ruby. Would anyone with sufficient knowledge care to eli15 why this is and how it is implemented ?

Ruby's regex engine is Onigmo which is very similar to Perl 5.10. Perl was one of Ruby's main influences, along with Smalltalk and Lisp, whereas Python's BDFL was never a fan of Perl. However, Perl's implementation of regular expressions (PCRE) has been widely adopted hence the similarity you refer to. If you compare with Javascript and PHP you'll find they're all similar.

Re: Show HN: I wrote a book on Python regular expressions

#40

There two kinds of text search/parse problems: - The really easy ones. A simple string search/split will do and a regex would be overkill. - The really hard ones. You'll need to fully parse this and using a regex will result in fragile/hard to understand code. Please don't use regexes in production software. Learn how to write simple parsing code.

> regex will result in fragile/hard to understand code

Sure, if you dont know what you're doing.

Post reply on HN