News for hackers. Stuff that matters.
Re:Show HN: I wrote a book on Python regular expressions by Maro on Thursday August 08, 2019 @12:52PM (#20646174)
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)
  )
Re:Show HN: I wrote a book on Python regular expressions by sweeneyrod on Thursday August 08, 2019 @02:26PM (#20647165)
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 by nerdponx on Thursday August 08, 2019 @09:01PM (#20650452)
And/or use verbose mode -- re.VERBOSE or (?x) --for comments and whitespace.
Re:Show HN: I wrote a book on Python regular expressions by jazzyjackson on Thursday August 08, 2019 @03:54PM (#20648019)
wow I didn't know you could concatenate regexes, thanks
Re:Show HN: I wrote a book on Python regular expressions by gnulinux on Thursday August 08, 2019 @04:04PM (#20648101)
Why not? They're just strings until they're compiled. "Code is data"
Re:Show HN: I wrote a book on Python regular expressions by CaptainMarvel on Thursday August 08, 2019 @05:19PM (#20648901)
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 by jazzyjackson on Thursday August 08, 2019 @10:18PM (#20650817)
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 by baudehlo on Friday August 09, 2019 @09:20AM (#20654016)
In Javascript you just need to wrap in `new RegExp(r1 + r2 + r3)`.
Re:Show HN: I wrote a book on Python regular expressions by Maro on Thursday August 08, 2019 @05:07PM (#20648789)
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 by asicsp on Thursday August 08, 2019 @11:35PM (#20651143)
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 by just_myles on Thursday August 08, 2019 @02:16PM (#20647052)
I 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)

Re:Show HN: I wrote a book on Python regular expressions by _eht on Thursday August 08, 2019 @03:16PM (#20647621)
You should allow “$” on the left; as per the RFC...
Re:Show HN: I wrote a book on Python regular expressions by just_myles on Thursday August 08, 2019 @03:52PM (#20647994)
Why I hate regex :) . You think you know it but, you never really do.
Re:Show HN: I wrote a book on Python regular expressions by nerdponx on Thursday August 08, 2019 @10:25PM (#20650851)
That's about the email RFC, not about regex syntax.
Re:Show HN: I wrote a book on Python regular expressions by just_myles on Friday August 09, 2019 @12:16PM (#20655704)
Ok cool.
Re:Show HN: I wrote a book on Python regular expressions by wwwhizz on Thursday August 08, 2019 @03:09PM (#20647560)
I would not allow the % sign.
Re:Show HN: I wrote a book on Python regular expressions by ape4 on Thursday August 08, 2019 @02:43PM (#20647329)
To me "exactly" means the same as "literal". (I understand here its a full pattern match)
Re:Show HN: I wrote a book on Python regular expressions by foo_in_bar on Thursday August 08, 2019 @01:17PM (#20646416)
Never heard about the rxe but it looks like something I could use - thanks.
Re:Show HN: I wrote a book on Python regular expressions by killaken2000 on Friday August 09, 2019 @01:21AM (#20651534)
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 by gnulinux on Thursday August 08, 2019 @01:31PM (#20646565)
Too verbose, I prefer good old regular expression.
Re:Show HN: I wrote a book on Python regular expressions by felixr on Thursday August 08, 2019 @02:25PM (#20647157)
Completely 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.
Re:Show HN: I wrote a book on Python regular expressions by PieUser on Thursday August 08, 2019 @01:41PM (#20646681)
This is excellent, I prefer this.
Re:Show HN: I wrote a book on Python regular expressions by qwsxyh on Thursday August 08, 2019 @02:07PM (#20646967)
10 lines to replace maybe 20 characters. How horribly verbose.
Re:Show HN: I wrote a book on Python regular expressions by tW4r on Thursday August 08, 2019 @02:24PM (#20647146)
But 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.
Re:Show HN: I wrote a book on Python regular expressions by mikorym on Thursday August 08, 2019 @02:53PM (#20647413)
This 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-t...

Re:Show HN: I wrote a book on Python regular expressions by ken on Thursday August 08, 2019 @05:36PM (#20649057)
That describes all of Python, to an APL programmer.