Live data from Hacker News

Show HN: I wrote a book on Python regular expressions

news.ycombinator.com

41–50 of 51 posts

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

#41

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 ?

Perl is a superior language. Besides being the fastest scripting language by far ...

Ruby was inspired by it.

Python stil can't do forward references, amongst many other puzzling limitations. I'm sure Python 3's split() improvements were copied from Perl.

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

#42

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"

Well I suppose the r in front of them in python made them look just enough "not a normal string" for me to forget how the + operator might act.

My day-to-day experience is in nodejs, where adding one regex object to another coerces them to normal strings first

[edit: hey neat, the hackernews form strips emoji from comments, I wonder if that's just ranges of unicode or if there's some crazy regex going on :D]

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

#44

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 don't have experience with parsers, so I cannot comment how easy/difficult it is compared to regex.

Regex can indeed lead to issues [1] but so could any other piece of code. So, I disagree that regex shouldn't be used in production. This article [2] by Jeff Atwood gives a balanced view of when to use regex and some nice tips.

[1] https://new.blog.cloudflare.com/details-of-the-cloudflare-ou...

[2] https://blog.codinghorror.com/regular-expressions-now-you-ha...

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

#45
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) )

I actually find these too verbose. May be because I didn't know about them when I started out, was using regex in cli tools and Perl scripts.

There's also a module [1] which already has collection of common regex to match dates, links, emails, etc.

[1] https://github.com/madisonmay/CommonRegex

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

#46
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) )

I'll have to look into this. In some limited cases verbosity that leads to a more obvious and understandable presentation is preferable, at least to me.

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

#47
post #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'r…

Regex is handy for client side JS validation of fields such as a field needs to be an integer etc. You could use parser combinator, but why combinators (pun!) when a simple regex will do?

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

#49

Earlier quoted context omitted.

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

Well I suppose the r in front of them in python made them look just enough "not a normal string" for me to forget how the + operator might act. My day-to-day experience is in nodejs, where adding one regex object to another coerces them to normal strings first [edit: hey neat, the hackernews form strips emoji from comments, I wonder if that's just ranges of unicode or if there's some crazy regex going on :D]

In Javascript you just need to wrap in `new RegExp(r1 + r2 + r3)`.
Post reply on HN