Slashdot Mirror


(Useful) Stupid Regex Tricks?

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?"

15 of 516 comments (clear)

  1. Regexp-based address validation by mutende · · Score: 5, Informative

    Beautiful regexp that validates RFC 822 addresses: Mail-RFC822-Address.html

    --
    Unselfish actions pay back better
  2. Windows by jgtg32a · · Score: 3, Informative

    MS Office does support regexp while not as good as Perl regex, they are very helpful.

    Link to and excel .bas addon for regexp, which helped me alot.
    Don't forget to add the lib {tools->References->MS VBA Scrip regexp 5.5}

    http://www.tmehta.com/regexp/using_functions.htm

  3. Re:IP and Hardware addresses by Richard_J_N · · 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.

  4. Re:IP and Hardware addresses by Speare · · Score: 5, Informative

    For pretty much any useful stock problem solved by regular expressions, see Perl's Regex::Common module. A lot of these patterns are fiendishly complicated to deal with edge-cases properly.

    --
    [ .sig file not found ]
  5. One regex to match them all by gzipped_tar · · Score: 4, Informative

    This regex matches a number: interger or float, scientific notation or plain, plus or minus...

    [-+]?(?:\b[0-9]+(?:\.[0-9]*)?|\.[0-9]+\b)(?:[eE][-+]?[0-9]+\b)?

    --
    Colorless green Cthulhu waits dreaming furiously.
  6. Do these questions really belong here? by DerCed · · 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

  7. recursive regexp to match {} block by doti · · Score: 3, Informative


          my $re = '';
            $re = qr/
                    \{ (?:
                            (?> [^{}]+ ) # nao-chaves
                    |
                            (??{ $re }) # sub-bloco de chaves
                    )* \} /xs;

    --
    factor 966971: 966971
  8. Re:IP and Hardware addresses by Bazzargh · · Score: 3, 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

  9. Re:Not a trick, but a question. by natebarney · · 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 *.

  10. Re:IP and Hardware addresses by fbjon · · Score: 4, Informative

    It seems both Opera and ping in Windows interpret individual parts with leading zeros as octal. More interestingly, Opera also accepts hexadecimal. That makes constructing a regexp that validates any arbitrary IP address, and not just a valid dot-decimal, a bit more cumbersome.

    --
    True confidence comes not from realising you are as good as your peers, but that your peers are as bad as you are.
  11. Re:IP and Hardware addresses by Arkaic · · Score: 3, Informative

    Also, when configuring ACLs 0.0.0.0 usually means all ip addresses.

  12. Re:Not a trick, but a question. by Moebius+Loop · · 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:

    /^(.*?)([FB][ot]o)((.+?)\2)+(.*?)$/

    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.

    --
    have you been seen on slash?
  13. Re:IP and Hardware addresses by akozakie · · Score: 3, 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.

  14. Re:IP and Hardware addresses by squallbsr · · Score: 3, Informative

    Also, typically binding a service to ip 0.0.0.0 connects it to all available interfaces on the machine.

    i.e: starting a development server for a django app on all interfaces (instead of the default 127.0.0.1)
    python manage.py -runserver 0.0.0.0:8000

    --
    Sleep: A completely inadequate substitution for Caffeine.
  15. Re:IP and Hardware addresses by tamyrlin · · Score: 4, 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