11 ms·
You might like: https://github.com/mtrencseni/rxe https://github.com/mtrencseni/rxe So you can write: username = rxe.one_or_more(rxe.set([rxe.alphanumeric(
by Maro 7y ago
You might like:
https://github.com/mtrencseni/rxe 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)
)
- just_myles 7y agoI use the below to extract email addresses. Took from some website some time ago and made super light changes to it. Not sure if this is the best but it has served me fairly well. email_reg = re.compile (r''' ([a-zA-Z0-9._%+-]+ #First name and last name @ #@ sign [a-zA-Z0-9._%+-]+ #domain name \.[a-zA-Z]{2,10}) #.com ''',re.VERBOSE)
- wwwhizz 7y agoI would not allow the % sign.
- _eht 7y agoYou should allow “$” on the left; as per the RFC...
- just_myles 7y agoWhy I hate regex :) . You think you know it but, you never really do.
- nerdponx 7y agoThat's about the email RFC, not about regex syntax.
- just_myles 7y agoOk cool.
- foo_in_bar 7y agoNever heard about the rxe but it looks like something I could use - thanks.
- gnulinux 7y agoToo verbose, I prefer good old regular expression.
- felixr 7y agoCompletely agree. If you know good old regular expressions, then this rxe definition is actually harder to read. This is how APL programmers must feel when they look at code in popular programming languages.
- PieUser 7y agoThis is excellent, I prefer this.
- qwsxyh 7y ago10 lines to replace maybe 20 characters. How horribly verbose.
- tW4r 7y agoBut that is exactly what I like about it. I rarely have to use regex, but when I do have to write or change something I always have to spend 15 mins reading (remembering) most of the things about it again, this would help me understand my old code way better.
- mikorym 7y agoThis is good point, and without trying to debate anything, regex is something of an art form for some people and I guess as many people are averse to it as there are high school people hating Shakespear. But to give a bit of substance, you can often use a dictionary type approach in a situation where regex is needed. Example: replacing accented latin with normal (ascii) latin. I do sometimes pride myself in necromancing skills of resurrection old Perl scripts from perlmonks.com but I suspect it is more of a hobby that out of absolute necessity. I find the memes about Perl/Python and Starwars to be pretty funny and much more entertaining than people actually debating programming languages. [1] [1] https://www.python.org/doc/humor/#python-vs-perl-according-to-yoda https://www.python.org/doc/humor/#python-vs-perl-according-t...
- ken 7y agoThat describes all of Python, to an APL programmer.
- sweeneyrod 7y agoThis 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.
- jazzyjackson 7y agowow I didn't know you could concatenate regexes, thanks
- gnulinux 7y agoWhy not? They're just strings until they're compiled. "Code is data"
- CaptainMarvel 7y agoThe thought of doing this literally didn’t occur to me... I think this is great!
- jazzyjackson 7y agoWell 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]
- baudehlo 7y agoIn Javascript you just need to wrap in `new RegExp(r1 + r2 + r3)`.
- Maro 7y ago
- ape4 7y agoTo me "exactly" means the same as "literal". (I understand here its a full pattern match)
- asicsp 7y agoI 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 https://github.com/madisonmay/CommonRegex
- killaken2000 7y agoI'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.