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.
Show HN: I wrote a book on Python regular expressions
31–40 of 51 posts
Re: Show HN: I wrote a book on Python regular expressions
#32Earlier 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
Re: Show HN: I wrote a book on Python regular expressions
#33You 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.
Re: Show HN: I wrote a book on Python regular expressions
#34Re: Show HN: I wrote a book on Python regular expressions
#35You 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.
Re: Show HN: I wrote a book on Python regular expressions
#36There 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.
Re: Show HN: I wrote a book on Python regular expressions
#37There 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'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
#38You 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.
Re: Show HN: I wrote a book on Python regular expressions
#39Earlier 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 ?
Re: Show HN: I wrote a book on Python regular expressions
#40There 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.
Sure, if you dont know what you're doing.