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…
My favorite regex of all time
61–70 of 117 posts
Re: My favorite regex of all time
#62Earlier 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.
Re: My favorite regex of all time
#63My 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.
http://news.ycombinator.com/item?id=1486502
full comment thread here http://news.ycombinator.com/item?id=1486158
Re: My favorite regex of all time
#64Can anyone explain how this regex [- ~] matches ASCII characters ?
The bracket expression [ ] defines single characters to match, however you can have more then 1 character inside which all will match.
[a] matches a
[ab] matches either a or b
[abc] matches either a or b or c
[a-c] matches either a or b or c.
The - allows us to define the range. You can just as easily use [abc] but for long sequences such as [a-z] consider it short hand.In this case [ -~] it means every character between and , which just happens to be all the ASCII printable characters (see chart in the article). The only bit you need to keep in mind is that is a character as well, and hence you can match on it.
You could rewrite the regex like so (note I haven't escaped or anything in this so its probably not valid)
[ !"#$%&'()*+,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~]
but that's not quite as clever or neat.Re: My favorite regex of all time
#65Earlier quoted context omitted.
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.
While I agree I believe this comment by _delirium sums this up rather well, http://news.ycombinator.com/item?id=1486502 full comment thread here http://news.ycombinator.com/item?id=1486158
I suppose I could accept "regex" as not being a regular expression as such, but the two are used so interchangeably that maintaining a distinction isn't very realistic. I'd personally rather a regular expression described a regular language, and "PCRE" (or so) used for the Turing-complete expressions with a similar syntax.
Re: My favorite regex of all time
#66Re: My favorite regex of all time
#67Earlier quoted context omitted.
Search "[ -~]" (with or without quotes) to see how good Google's comment is.
The only ambiguous thing about this regex is knowing what's between space and tilde. Otherwise this is a pretty ordinary regex.
Re: My favorite regex of all time
#68Earlier quoted context omitted.
Thank you for that. You have no idea how annoying it is to port perl scripts from ASCII to EBCDIC when they do that kind of thing.
It's not an ASCII v EBCDIC thing, its an ASCII vs Unicode thing.
Re: My favorite regex of all time
#69As 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.
You are right anyway.
Re: My favorite regex of all time
#70As 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.