Live data from Hacker News

My favorite regex of all time

catonmat.net

71–80 of 117 posts

Re: My favorite regex of all time

#71

Earlier quoted context omitted.

For file names, URLs, domain names, etc. it's usually the safe thing to do.

Who's filenames aren't unicode? Also domains and URLs can be unicode too.

> Who's filenames aren't unicode?

Many filesystems don't support unicode or support only a subset of it:

https://en.wikipedia.org/wiki/Filename#Comparison_of_filenam...

> Also domains and URLs can be unicode too.

Domains: it depends at which level you are dealing with them. See https://en.wikipedia.org/wiki/Internationalized_domain_name

    Internationalized domain names are stored in the Domain 
    Name System as ASCII strings using Punycode transcription. 
URLs: Unicode characters are not allowed in URLs. See http://www.faqs.org/rfcs/rfc1738.html and http://www.blooberry.com/indexdot/html/topics/urlencoding.ht...

    only alphanumerics, the special characters "$-_.+!*'(),", and
    reserved characters used for their reserved purposes may be used
    unencoded within a URL.

Re: My favorite regex of all time

#72
post #62
post #45

Earlier quoted context omitted.

I'm saying that usually comments are either wrong or out of date, developers code one regex, comment it, then fix a bug later and don't, then there's a discrepancy between the comment and the code. It's nearly always easier to just google the code and see what it does, if (as in this case) it's not obvious.

Your response doesn't address what citricsquid said, googling for a regex will almost never return helpful results.

Google regex and you'll find plenty of resources including tools to testing patterns. You won't find much for any specific pattern but read the docs and it will be apparent what this regex does. Familiariy and competence with regex is a basic component of being a developer.

Re: My favorite regex of all time

#73
post #57
post #2

Are people seriously still deliberately using ASCII-reliant code?

Every time I've had to deal with Unicode and internationalization, it's been a problem. For example, a few years ago I grabbed a source tarball from somewhere, I forget what or where. It had the author's name in a comment, which included an O with dots over it. That was the only non-ASCII character in the source code. No matter what I did, both Eclipse and command-line javac refused to compile the source. Finally I w…

> moon runes (Chinese/Japanese/Korean characters)

Really? You can make your economic point without making fun of several of the world's most commonly spoken languages and the people who speak them.

Re: My favorite regex of all time

#74
post #16

As someone who makes much of his living rehabilitating old perl scripts, please, if you must use such things, use them like this: [ -~] #match only printable characters It takes 5 seconds longer and with regexes, just knowing what the damn thing is trying to do is half the battle. When you use a regex, use a comment. Its the civil thing to do.

Google is by far the best "comment"

The best code is readable. Readability includes comments. If you're going to comment anything in your code at all, RegExes should be at the very top of that list.

Even if I can figure out what the regex matches (with Google or something else), that doesn't necessarily tell me WHY I'm matching on that particular pattern, or why I needed a RegEx in this spot, or what the intent was at the time of writing it.

Re: My favorite regex of all time

#76
post #5

This will not only miss non-ascii printing characters, but it's not even much shorter than typing [[:print:]] to use the explicit character class.

The [[:print:]] will match any printable characters like åä, while the [ -~] will not. I used this once as another safeguard against pushing binary data into the database. It was a poor system to begin with where you even have that possibility... and it happened at least once before the fix and my safeguard was in place.

"å" is perfectly valid text input in my locale.

Re: My favorite regex of all time

#77
post #67
post #37

Earlier quoted context omitted.

The only ambiguous thing about this regex is knowing what's between space and tilde. Otherwise this is a pretty ordinary regex.

The only thing ambiguous about it is most of it?

Not that I agree with the "expect people reading your code to Google things" mindset, but to be fair the only ambiguous thing is the ASCII table which is Googleable.

Re: My favorite regex of all time

#78
post #57
post #2

Are people seriously still deliberately using ASCII-reliant code?

Every time I've had to deal with Unicode and internationalization, it's been a problem. For example, a few years ago I grabbed a source tarball from somewhere, I forget what or where. It had the author's name in a comment, which included an O with dots over it. That was the only non-ASCII character in the source code. No matter what I did, both Eclipse and command-line javac refused to compile the source. Finally I w…

I assume this is a not very subtle troll? Java source is unicode? (The offhand reference to dd and xargs is a bit too much).

How do you define "English-speaking world", btw? Those too ignorant to have heard of non-ascii-characters (ie: excluding Canada, as anyone doing business there should at least have heard of French)?

Anyway, for anyone actually burnt by something similar on a GNU system try looking up recode(1).

Re: My favorite regex of all time

#79
post #38

[\p{L&}] <- unicode version, in case you were wondering.

No, you don't need '[' and ']' ? Also you don't need the '&' to get the equivalent of the above, but might have to add support for spaces?

  > \p{L} or \p{Letter}: any kind of letter from any language.
vs

  > \p{L&} or \p{Letter&}: a letter that exists in lowercase and uppercase variants (combination of Ll, Lu and Lt).
Along with:

  > \p{Z} or \p{Separator}: any kind of whitespace or invisible separator.
Considering the op matches everything printable, including whitespace (or actually just space, not tab), numbers and punctation, I think the equivalent would be "\X" ?

All this based on glancing at:

   http://www.regular-expressions.info/unicode.html

Re: My favorite regex of all time

#80
post #76

Earlier quoted context omitted.

The [[:print:]] will match any printable characters like åä, while the [ -~] will not. I used this once as another safeguard against pushing binary data into the database. It was a poor system to begin with where you even have that possibility... and it happened at least once before the fix and my safeguard was in place.

"å" is perfectly valid text input in my locale.

I think that was his point, that he had a good use for :print: over just -~
Post reply on HN