Live data from Hacker News

My favorite regex of all time

catonmat.net

51–60 of 117 posts

Re: My favorite regex of all time

#51
post #44

I suppose a single regex can be both "favorite" and "worst" at the same time... it's only slightly interesting to know where ~ appears in the ASCII character set, and while someone might recall that space is kinda near the beginning but after the control characters, is it the first helpful printable character? Who knows?

> I suppose a single regex can be both "favorite" and "worst" at the same time...

We definitely aren't the only ones who appreciate horrible things.

INTERCAL comes to mind here.

Re: My favorite regex of all time

#52
post #10

Earlier quoted context omitted.

Unless you want to seem clever and impresse the PHB. It is the selfish (but smart) thing to do.

I'm not sure who the PHB is, but I'm certainly not impressed by anything cryptic in a codebase. Deliberately writing code that's hard to understand should be a firing offense.

PHB: Pointy Haired Boss

Re: My favorite regex of all time

#55
post #30

My favorite regex is the following, /^1?$|^(11+?)\1+$/ Which finds prime numbers. Although, I can't for the life of me think of a reason for using it. http://stackoverflow.com/questions/3296050/how-does-this-reg...

I had to prove that in a formal languages class once and I still have no idea how it works.

Re: My favorite regex of all time

#56
post #45

Earlier quoted context omitted.

Are you saying people should google regular expressions? in my experience (correct me if I'm wrong) that doesn't work, I've never been able to get google to return relevant results even with quotation marks.

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.

or make a function regexMatchingAllPrintableASCIIChars() and have it return the regex.

Re: My favorite regex of all time

#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 wrote a script to delete his name from every source file manually. It compiled flawlessly.

Then there's the time I found some text files with two characters of binary junk at the beginning, followed by completely normal text. Again, I forget what I was doing, but some program was refusing to process them correctly. It was something internationalization-related called the BOM. Eventually I ended up writing a script to walk a directory and remove the first two bytes of every file. (This can probably be done with dd and xargs on UNIX, but I was using Windows at the time, which means that something like this will require spending an hour or so in your favorite programming language.)

These experiences lead me to believe that, for bootstrapped USA startups at least, you shouldn't worry about a market outside the English-speaking world.

If you need to worry about junk like accented characters or moon runes (Chinese/Japanese/Korean characters), it means you're big enough to afford to hire someone specifically to address the problem.

Re: My favorite regex of all time

#58
post #6

This works for ASCII only, use unicode character classes instead.

That only matters if you need to process Unicode.

See my comments [1] [2] [3] for why Unicode / internationalization should be avoided.

[1] http://news.ycombinator.com/item?id=4369323

[2] http://news.ycombinator.com/item?id=4541039

[3] http://news.ycombinator.com/item?id=4775440

Re: My favorite regex of all time

#60
post #30

My favorite regex is the following, /^1?$|^(11+?)\1+$/ Which finds prime numbers. Although, I can't for the life of me think of a reason for using it. http://stackoverflow.com/questions/3296050/how-does-this-reg...

I do dislike people calling that expression a "regex", because it isn't: regular expressions cannot contain backreferences, and must be computable in linear time, whereas primality tests are polynomial.
Post reply on HN