(Useful) Stupid Regex Tricks? 516
careysb writes to mention that in the same vein as '*nix tricks' and 'VIM tricks', it would be nice to see one on regular expressions and the programs that use them. What amazingly cool tricks have people discovered with respect to regular expressions in everyday life as a developer or power user?"
Regexp-based address validation (Score:5, Informative)
Windows (Score:4, Informative)
Link to and excel
Don't forget to add the lib {tools->References->MS VBA Scrip regexp 5.5}
http://www.tmehta.com/regexp/using_functions.htm [tmehta.com]
Re:IP and Hardware addresses (Score:5, Informative)
Of course, you can do better still. For mac addresses, try:
^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$
[:xdigit:] is short for hexadecimal digits, i.e. a-fA-F0-9
We can also loop 5 times over the 'XX:' sections.
Re:IP and Hardware addresses (Score:5, Informative)
For pretty much any useful stock problem solved by regular expressions, see Perl's Regex::Common [cpan.org] module. A lot of these patterns are fiendishly complicated to deal with edge-cases properly.
One regex to match them all (Score:5, Informative)
[-+]?(?:\b[0-9]+(?:\.[0-9]*)?|\.[0-9]+\b)(?:[eE][-+]?[0-9]+\b)?
news for nerds. NERDS (Score:3, Informative)
stuff that matters
understand the concept?
if not, try going to this site [tmz.com], it looks like it might be more your speed
buhbye
Do these questions really belong here? (Score:5, Informative)
I wonder why such FAQs are still posted on a site like Slashdot. We now have a great repository for exactly this kind of questions:
http://stackoverflow.com/questions/tagged?tagnames=regex&sort=votes&pagesize=15 [stackoverflow.com]
Just read this (Score:2, Informative)
RFC 822 email validation (Score:2, Informative)
Cal Henderson's routine is the best RFC compliant regex I have ever found to verify an email address:
http://code.iamcal.com/php/rfc822/ [iamcal.com]
recursive regexp to match {} block (Score:4, Informative)
my $re = '';
$re = qr/
\{ (?:
(?> [^{}]+ ) # nao-chaves
|
(??{ $re }) # sub-bloco de chaves
)* \}
Re:Regexp-based address validation (Score:2, Informative)
I matches the entire RFC, not just the you@slashie.com .
<You @ Slashie> you@slashie.com
Should be valid if I remember correctly.
Re:PCRE and perl 5.10 offer "tagged" captures (Score:2, Informative)
That's also one of my favorite. Python has this feature too.
Handy links (Score:3, Informative)
http://www.regular-expressions.info/ [regular-expressions.info] - this one is handy for regex info particularly in Javascript which I use so infrequently I need to know how to match, capture, substitute, etc.
http://perldoc.perl.org/perlre.html [perl.org] - plenty of regex info there which is Perl specific, but of course extends to many other similar implementations
Re:IP and Hardware addresses (Score:4, Informative)
/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/
Try this: /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.|$)){4}/
And similarly: /^(([0-9a-fA-F]{2})(:|$)){6}$/
(term(delimiter|$)){n} is the generic stupid regex trick here. Works in perl, ymmv elsewhere.
-Baz
Blasphemy (Score:3, Informative)
Re:Not a trick, but a question. (Score:4, Informative)
Magic stuff like this is not working: /\([FB][ot]o\).*\1/ although that seems to be the closest description of what we wanted.
In perl, I did /([FB][ot][o]).*\1/ and it seemed to work as you wanted. Also, if you're using a regex engine that supports lazy (non-greedy) quantifiers like perl does, I would use them in this case. It reduces backtracking. In perl, put a ? after the *.
Re:IP and Hardware addresses (Score:5, Informative)
Re:IP and Hardware addresses (Score:3, Informative)
Re:Not a trick, but a question. (Score:3, Informative)
In most regex engines, you should be able to do this with backreferences. I don't use them often, but I think something like this would work:
I think the reason the example you gave using \1 didn't work is because the .* was too greedy, and ate up the rest of the pattern before the \1 got a chance to match. Also, when you're doing full line matching, it's always good to think about ^/$ and whether you're using any multiline modifiers.
Re:very topical! (Score:2, Informative)
This is easy once you re-word your definition of a word: in your case, a word starts with a capital followed by a run of non-capital letters.
The regex: /[A-Z][a-z]*/
Will match the first of such words in a string. (it will also match single-character words; change * to + if you don't want that). Make sure you're matching is case-sensitive for this to work. Many regex engines will have an abbreviation for [A-Z] and [a-z] you can use instead.
To get the second of such words in string: /[A-Z][a-z]*[^A-Z]*([A-Z][a-z]*)/
The second word will be in the first sub-match (\1). The [^A-Z]* will gobble up everything between the last letter of the first word and the start of the second word. If there is no second word, this match will fail.
Repeat the first part of this (everything up to the open parenthesis) to get third word, fourth word, etc. Rather than repeating that part of the expression, you can use parenthesis and counts (usually {n,m}) for this in most engines.
Re:Handy links (Score:3, Informative)
I was recently trying to come up with a regex for some renaming file thingy recently, and I found I could easily state is pseudo-code what I wanted to do, but looking through and regex sites/tips/FAQs quickly went from "here's a very simple match test" to "going to the moon", with little in-between, which is what I was after.
However, I eventually found Reggy [apple.com] for OS X, a handy little tool for testing regexes, so all was not lost.
Re:IP and Hardware addresses (Score:4, Informative)
According to the RFC leading zeros specify octal and 0x is hexadecimal. Both are standard, but rarely used and not all programs support them. There are even more ways to write an IP address, including dword and different mixes, but they are usually only used for obfuscation in malware.
Re:IP and Hardware addresses (Score:3, Informative)
i.e: starting a development server for a django [djangopowered.com] app on all interfaces (instead of the default 127.0.0.1)
python manage.py -runserver 0.0.0.0:8000
OK, I'll play... (Score:3, Informative)
Bad filename character for Windows (if it matches, the filename is invalid):
E-mail (use case insensitive):
GUID (use case insensitive):
IP on local private network:
Removes .NET named capture syntax so that a .NET Regex string can be used elsewhere (such as Javascript) (replace with nothing):
Flame away about how horrible it is that I missed some edge case that even nobody on Slashdot has ever heard of, but they work well for me and hopefully for you too.
Now, if you actually find a common case that I missed, I would appreciate the help...
Re:Regex Support (Score:2, Informative)
Re:Not a trick, but a question. (Score:2, Informative)
The only reason /\([FB][ot]o\).*\1/ doesn't work is because you escaped the parens. It works in my compiler as /([FB][ot]o).*\1/
HTH
Re:is it an rfc-822 compliant e-mail address? (Score:2, Informative)
Re:IP and Hardware addresses (Score:5, Informative)
I personally like the regex-builder mode in Emacs as well. This one allows you to build a regexp while highlighting all matches in the current buffer.
Of course, this should probably have been posted in the emacs thread earlier, but I think it is probably a good match for this thread as well :)
To start it, just use M-x regexp-builder
Re:Your re is overrated (Score:2, Informative)
cause it's an interesting discussion of a common (mis)understanding. did you know the RFC specifies leading-zero-for-octal and leading-0x-for-hex? i knew those were commonly used conventions in some places but didn't know that included IP addresses.
if the mods do their job, the posts correcting the GP's mistaken understanding will also score high marks.