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.
My favorite regex of all time
91–100 of 117 posts
Re: My favorite regex of all time
#92Are 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…
http://en.wikipedia.org/wiki/UTF-8#Byte_order_mark
And personally I think to exclude all internationalisations because they're harder is a terrible attitude to have. Particularly these days when there's an online tutorials for pretty much any job imaginable (not to mention the numbers of helpful experts willing to give up their time for free on various forums and communities).
Re: My favorite regex of all time
#93Earlier quoted context omitted.
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 stri…
Re: My favorite regex of all time
#94Can anyone explain how this regex [- ~] matches ASCII characters ?
Re: My favorite regex of all time
#95Re: My favorite regex of all time
#96Earlier quoted context omitted.
It's not just Unicode either. I just mentioned EBCDIC because that particular regex has bit me before when I was translating perl scripts from Linux to zOS USS. Take a look at the code page for EBCDIC, you'll see quickly why it's a massive pain to sort through regexes like that.
I honestly thought you were being sarcastic. I've never heard of someone who has actually used EBCDIC.
Re: My favorite regex of all time
#97My 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.
The first part (^1?$) allows "" and "1" to match (so that 1 is not detected as a prime).
The second part matches groups of two or more ones (11+?), repeated twice or more, ie products n*m, n ≥ 2, m ≥ 2.
The backreference means that \1 should match the exact same string as the first (11+?). It's different from using (11+?){2,} which would match n_1+n_2+n_3..., n_1 ≥ 2, n_2 ≥ 2, n_3 ≥ 2 (where submatch is independent).
Re: My favorite regex of all time
#98Earlier quoted context omitted.
Yea, anytime I use a regex that isn't immediately obvious I put it in a function called get_ . Unfortunately people that write overly complicated and error prone regexes usually don't choose to document them.
If a regex is going to be reusable, then yeah, I'd agree. But dumping single lines of code into their own functions just for readability isn't practical for real time systems. In those cases you really should be using comments as they get stripped out by the compiler.
I do agree that it might be overkill to move regexes to their own functions just for readability's sake but I don't buy the performance argument. Furthermore, regexes are most popular in scripting languages that no sane person would use for real time performance-critical systems anyway.
Re: My favorite regex of all time
#99Are 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…
Ok, this is where I stop worrying about how quickly I write code. Did this (removing BOM) quite a few times and it took just a few minutes in Python (under Windows). Heck, this could be two-liner I think :)
Re: My favorite regex of all time
#100Earlier 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.
Email could be an example, I guess, although I haven't worked with it enough to know whether the whole "7-bits only" thing is still an issue these days.