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

516 comments

  1. IP and Hardware addresses by rallymatte · · Score: 5, Insightful

    To filter a string to make sure it's a valid ip address this regexp is quite useful.
    /^((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])$/

    And this one for mac addresses
    /^[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}$/

    1. Re:IP and Hardware addresses by fbjon · · Score: 2

      Are IP adresses with leading zeroes usually considered invalid?

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

      So if I get this right, 0.0.0.0 is a valid ip address? I know the real regex would take a full post, but yes, it is possible to check with a single regex is it is valid, if it makes sense (127.0.0.1, 10.*, 169.254.*, etc etc) and if it's not a broadcast or a network address (not taking netmask into account).

    3. Re:IP and Hardware addresses by tfeserver · · Score: 1

      a "small" date verification /^(?:(?:(?:(?:[0-2][0-9]?)|(?:3[0-1]))\/(?:(?:0?[13578])|(?:1[02])))| (?:(?:(?:[0-2][0-9]?)|30)\/(?:(?:0?[469])|11))|(?:(?:(?:[0-1][0-9]?)| 2[0-8])\/(?:0?2)))\/\d{2}(?:\d{2})?\s+(?:[0-1]?[0-9]|2[0-4]):(?:[0-5]?[0-9])$/x

    4. 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.

    5. 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 ]
    6. Re:IP and Hardware addresses by david.given · · Score: 1

      ^((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])$

      I'm not sure this is valid --- it doesn't accept non-dotted IP addresses, does it? i.e. expressing 127.0.0.1 as 2130706433. (Or 127.1, but which is equally, and surprisingly, valid.)

    7. Re:IP and Hardware addresses by rallymatte · · Score: 5, Funny

      Not only are you showing off with a lower member id than me, do you also have to come up with a cooler regexp than me?

    8. Re:IP and Hardware addresses by 1s44c · · Score: 1

      To filter a string to make sure it's a valid ip address this regexp is quite useful. /^((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])$/

      It really doesn't look like that will match all valid IPs, although it is very though. Surely 010.000.000.001
      is still valid?

      I normally use something like: /^\([0-2]\{0,1\}[0-9]\{1,2\}\.\)\{3\}[0-2]\{0,1\}[0-9]\{1,2\}$/
      ( sed style, not perl style. Perl style would of course be shorter. )

    9. Re:IP and Hardware addresses by alta · · Score: 4, Funny

      I can easily beat you on the UID, but I couldn't regex the a out of an apple.

      --
      Do not meddle in the affairs of sysadmins, for they are subtle, and quick to anger.
    10. 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

    11. Re:IP and Hardware addresses by LordKronos · · Score: 1

      And this one for mac addresses /^[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}$/

      Wouldn't it be nicer if you did: /^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$/

    12. Re:IP and Hardware addresses by plumby · · Score: 3, Insightful

      So if I get this right, 0.0.0.0 is a valid ip address?

      If you mean "Is it an address that you can send IP traffic to?", then the answer is no. If you mean "Is it a valid value that can end up in an IP address field (e.g., in the response to the ipconfig command)?" then the answer is yes - it means that you've not got a connection.

    13. Re:IP and Hardware addresses by Bazzargh · · Score: 1

      gah, obviously that matches an extra . - brainfart. /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.|$)){4}(?!\.)/

      avoids this, still without repeating the term pattern. That last bit is the perlre for a zero-width negative look-behind assertion.

      -Baz

    14. Re:IP and Hardware addresses by LordKronos · · Score: 1

      I'm not sure what the standard, but 127.0.000000.1 get to my web server in firefox, ie, safari, opera, and chrome. So it might be better to do: /^(0*(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}0*(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/

    15. Re:IP and Hardware addresses by nschubach · · Score: 4, Interesting

      There's a really cool little "real time" regex analyzer written in Flex: (if you're not one of them scared to death by Flash content)

      http://gskinner.com/RegExr/

      Maybe you can monkey your way into "regexing" the a out of apple :p

      --
      Every time I start to have faith in humanity, I ruin it by driving to work between 7 and 8 am.
    16. Re:IP and Hardware addresses by neoform · · Score: 1

      I tried posting them right into the window, but slashdot's fantastic filters labeled the regex's as "junk characters"..

      http://city17.ca/regex.txt

      Here's a bunch of filters I've used (in PHP).

      On a side note, here's some fantastic PHP code I found:

      $query_login="select * FROM user";
      $result_login = mysql_query($query_login) or die("Query failed"); //$login_check = mysql_num_rows($result_login);

      while($row=mysql_fetch_array($result_login))
      {
      $username=$row["username"];

        if ($username==$username1)
        {
              echo "";
      echo "window.location.href='login_error.php?rec=qq';";
      echo "";
              exit;
          }
      }

      --
      MABASPLOOM!
    17. Re:IP and Hardware addresses by the+cleaner · · Score: 1

      Wow. It's unrelated, but how many of us first-registerers are still around?

      --
      Could be worse. Could be raining.
    18. Re:IP and Hardware addresses by whatUrunning.com · · Score: 0

      This one is quite good too: http://www.radsoftware.com.au/regexdesigner/

    19. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      And yet, a lone period is still matched as a valid number.

    20. 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.
    21. Re:IP and Hardware addresses by Nick · · Score: 1

      me too

      --
      Fuck Ajit Pai
    22. Re:IP and Hardware addresses by Arkaic · · Score: 3, Informative

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

    23. Re:IP and Hardware addresses by Nick · · Score: 1

      I was wondering the same thing. Always have been to lazy to get the ed staff to post about it and see how many reply.

      --
      Fuck Ajit Pai
    24. Re:IP and Hardware addresses by sqldr · · Score: 4, Funny

      Not only are you showing off with a lower member id than me

      Low ID = old fart. He may be a regexp wizard, but he probably looks like gandalf too :-D

      --
      I wrote my first program at the age of six, and I still can't work out how this website works.
    25. Re:IP and Hardware addresses by wertigon · · Score: 3, Funny

      Write a Regex for them! :D

      --
      systemd is not an init system. It's a GNU replacement.
    26. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      Not exactly invalid, but leading zeroes may be interpreted as octal.

      You wouldn't want to use an IP address that one computer interprets as decimal, and another as octal.

    27. Re:IP and Hardware addresses by Bandman · · Score: 1

      You've pretty much got to take netmask into account. Lots of valid IPs end with 0 and 255.

    28. Re:IP and Hardware addresses by Bandman · · Score: 1

      That's just wrong and abusive of the system. Great for freaking people out, though

    29. Re:IP and Hardware addresses by JavaBear · · Score: 1

      Gaah, did you sign up in the launch date to get that low a member id??

    30. Re:IP and Hardware addresses by Dagger2 · · Score: 4, Funny

      That also fails beautifully with an address like "2001:db8:3c4d:48:a00:20ff:feb9:4c54", which is perfectly valid.

      Unless you know you're going to be dealing with numeric IPv4 addresses in a specific format, it would be best to pass them to getaddrinfo() (with AI_NUMERICHOST if you want to avoid DNS) and let somebody else worry about validating them properly.

    31. 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.

    32. Re:IP and Hardware addresses by jbezorg · · Score: 1

      I know they can use some improvements because some ranges are missing, but here's what I've got.

      ARIN
      /^(24|6[3-9]|7[0-6]|9[6-9]|199|20[4-9]|216)\./

      APNIC
      /^(5[8-9]|6[0-1]|11[4-9]|12[0-6]|20[2-3]|21[0-1]|21[8-9]|22[0-2])\./

      RIPE
      /^(62|7[7-9]|8[0-9]|9[0-5]|19[3-5]|21[2-3]|217)\./

      LACNIC
      /^(18[6-7]|189|190|20[0-1])\./

      MISC ARIN
      /^(12[8-9]|13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-2]|188|19[1-2]|198)\./

      --
      I've lost all my marbles except one & It's fun to test angular & centripetal acceleration in my skull
    33. Re:IP and Hardware addresses by jbezorg · · Score: 1

      Missed

      AfriNIC
      /^(41|196)\./

      --
      I've lost all my marbles except one & It's fun to test angular & centripetal acceleration in my skull
    34. 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.
    35. Re:IP and Hardware addresses by L4t3r4lu5 · · Score: 4, Funny

      That last bit is the perlre for a zero-width negative look-behind assertion

      It certainly looks like English, but I have no idea what that means. Whatever it is, it sure seems to help cure insomnia.

      --
      Finally had enough. Come see us over at https://soylentnews.org/
    36. Re:IP and Hardware addresses by Poltras · · Score: 1

      You can use the global rules for netmask. 1-127 is 0xFF000000, 128-192 is 0xFFFF0000, etc. Then, you cannot take into account the netmask used inside those ranges, since netadmins can do almost whatever they want with those.

      Anyway, no regex however long they are will check every possibility... so better use a script along that.

    37. Re:IP and Hardware addresses by Poltras · · Score: 1

      They might have the "last time logged in" column in their db. Dunno why they don't take out those who haven't logged in x (say 5) years and give people (say me) those accounts. I was reading slashdot back then, damnit. Was just too lazy to create an account...

    38. Re:IP and Hardware addresses by badc0ffee · · Score: 1

      Not invalid, but different than what you expect. A leading zero in an ip address implies octal rather than decimal number base. An ip of 216.34.181.45 is not the same as 216.034.181.045, one gets you nowhere and the other gets you spam, or vice versa.

      --
      1011 1010 1101 1100 0000 1111 1111 1110 1110
    39. Re:IP and Hardware addresses by jbezorg · · Score: 1
      And digging through some old notes, I found my source material.

      http://www.iana.org/assignments/ipv4-address-space/

      --
      I've lost all my marbles except one & It's fun to test angular & centripetal acceleration in my skull
    40. Re:IP and Hardware addresses by josecanuc · · Score: 2, Interesting

      There's quite a few. I mostly lurk, occasionally post when the topic is something I know well or if I have a snarky comment.

    41. Re:IP and Hardware addresses by glavenoid · · Score: 2, Funny

      No point, really. You old timers always seem to come out of the woodwork whenever low-UIDs come up in conversation :-)

      --
      I, for one, am looking forward to the inevitable /. beta rollout fallout.
    42. Re:IP and Hardware addresses by Danny+Rathjens · · Score: 1

      ifconfig accepts non-decimal numbers. So you can technically use ip addresses with 0x and 0 prefixes to indicate hex and octal numbers. e.g. 0x10.010.10.1 = 16.8.10.1. I got caught once with allowing people to enter addresses more freely like that and much confusion ensued when someone entered 010 as an octet not expecting it to mean 8 instead of 10. :)

    43. Re:IP and Hardware addresses by LordKronos · · Score: 2, Interesting

      Oh, wow, you are right. Using 0177.0.0.1 in firefox gets you to localhost, as does 0x7f.0.0.1

      Nice catch.

    44. Re:IP and Hardware addresses by Nick · · Score: 1

      honestly i went through a period of about 3 years where i didn't log into this account :P

      --
      Fuck Ajit Pai
    45. Re:IP and Hardware addresses by ConceptJunkie · · Score: 1

      I wish a "+1 snarky" mod affacted your karma.

      --
      You are in a maze of twisty little passages, all alike.
    46. Re:IP and Hardware addresses by Requiem18th · · Score: 1

      Wow that's the lowest uid I've ever seen in /.! How old are you if its not too personal?

      --
      But... the future refused to change.
    47. Re:IP and Hardware addresses by josecanuc · · Score: 1

      Remember when karma was a number you could see in your preferences page?

    48. Re:IP and Hardware addresses by networkBoy · · Score: 1

      I had an account, but forgot the UID/password and the e-mail address it was attached to is now someone else's domain...

      --
      whois gawk date unzip strip find touch finger mount join nice man top fsck grep eject more yes exit umount sleep dump
    49. Re:IP and Hardware addresses by ConceptJunkie · · Score: 1

      Yes. It took me about a month or two to hit 50, which was the max, IIRC. Shortly after that it just became "Excellent". I can understand wanting to prevent people from karma-whoring, but I miss the number.

      --
      You are in a maze of twisty little passages, all alike.
    50. Re:IP and Hardware addresses by multipartmixed · · Score: 1

      I actually use that file as a source for a regexp list I use with egrep -f.

      I wget and sed it, then use it.

      Messed me up a few months ago when the file format changed. *grumble*

      --

      Do daemons dream of electric sleep()?
    51. Re:IP and Hardware addresses by josecanuc · · Score: 3, Interesting

      Folks who think a low ID means a old person: get real. Slashdot hasn't been around forever. It started in 1997. Accounts were added later.

      Folks with a low ID just happened to register within the few months following the addition of accounts. Must have been 1998 or 1999. I was in college at the time. I'm currently not yet 30 years old. Is that old to you?

    52. Re:IP and Hardware addresses by RangerRick98 · · Score: 1

      The lowest I've seen is 56, but that belongs to someone who bought it from the original owner, IIRC.

      --
      "You're older than you've ever been, and now you're even older."
    53. Re:IP and Hardware addresses by kimba · · Score: 3, Insightful

      Why isn't 0.0.0.0 or 10.* a valid IP address? Since when is the definition of IP address to be unicast and globally routable?

      I'd rather take issue with the fact it completely fails on IPv6 addresses.

    54. Re:IP and Hardware addresses by Frederic54 · · Score: 1

      same here, I lurk often but I don't have time to read everything and reply

      --
      "Science will win because it works." - Stephen Hawking
    55. Re:IP and Hardware addresses by Frederic54 · · Score: 1

      And before this there was no "max", remember signal11?

      --
      "Science will win because it works." - Stephen Hawking
    56. Re:IP and Hardware addresses by Requiem18th · · Score: 1

      Younger than you, and thanks to your reality check I feel even more younger myself :P

        See I'm even using cutesy emoticons! It's like a god damn fountain of aol youth.

      --
      But... the future refused to change.
    57. Re:IP and Hardware addresses by josecanuc · · Score: 1

      Damn you! Please let me hold onto my youth!

    58. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      Yes, every IP address from 0.0.0.0 to 255.255.255.255 is valid IP address. If it makes sense to you or not is not an issue.

    59. Re:IP and Hardware addresses by halcyon1234 · · Score: 1

      ere's a couple more that will turn a string (from, say, a querystring) into another datatype.

      (And yes, you could use something like Int32.TryParse, but then you'd have to know in advance it was an integer.)

      Signed integer

      ^(?:(-)|(\+{0,1}))(?:\d{1,9}|[0-1]\d{9}|20\d{8}|21[0-3]\d{7}|214[0-6]\d{6}|2147[0-3]\d{5}|21474[0-7]\d{4}|214748[0-2]\d{3}|2147483[0-5]\d{2}|21474836[0-3]\d{1}|214748364(?(1)([0-8])|([0-7])))$

      Date, accounting for leapyears (gratuitously stolen from several other authors and stitched together like I was Dr. Frankenstein), in some format that will either make Americans happy or non-Americans happy. I can't remember.

      ^(?((?:(?:1[6-9]|[2-9]\d)(?:[02468][48]|[2468][048]|[13579][26]))|(?:(?:16|[2468][048]|[3579][26])00))(\d{4}-(?:(0[469]|11)-(0[1-9]|[1-2][0-9]|30)|(0[13578]|12)-(0[1-9]|[1-2][0-9]|31)|02-(0[1-9]|[12][0-9])))|(\d{4}-(?:(0[469]|11)-(0[1-9]|[1-2][0-9]|30)|(0[13578]|12)-(0[1-9]|[1-2][0-9]|31)|02-(0[1-9]|1\d|2[0-8]))))(?: (?:[01]\d|2[0-3]):(?:[0-5]\d):(?:[0-5]\d)){0,1}$

      Float (I think it was posted earlier, but duplication of code is never a bad thing, right? right?

      ^([+-]?((([0-9]+(\.)?)|([0-9]*\.[0-9]+))([eE]([+-]?\d{1,2}|[+-]?1[0-1]\d|[+]?12[0-7]|-12[0-6]))?))$

      GUID, with or without the squigglies

      ^(\{)?[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(?(1)(\}))$

      And most "useful", string and bool (adjust for your desired length of String or value of True):

      ^.{1,4000}$
      ^(True|False)$

    60. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      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.

      ([0-9]{1,3}\.){3,}[0-9]{1,3}

      Accepts 4,maximum of 3 digit numbers interleaved with .

      Still have to validate the 3Digit numbers in the range 0-255. But for my purposes it was useful.

    61. Re:IP and Hardware addresses by Rhys · · Score: 1

      I prefer the following IP-matcher. Shorter, but may not be more efficient for the RE engine to process (I didn't do the analysis). Accepts leading-0s (despite them being uncommon 001.001.001.001 should still read as valid). Doesn't accept 0001, but I'd rather throw that out (something went wrong if you generated a 4-digit ip fragment).

      ^(?!.*(25[6-9]|2[6-9]\d|[3-9]\d\d))(\d{1,3}\.){3}\d{1,3}$

      --
      Slashdot Patriotism: We Support our Dupes!
    62. Re:IP and Hardware addresses by Daimanta · · Score: 1

      Well, if you want to be exact, you need to contruct a big NFA-lambda and convert it to a DFA and then to a regexp. Doing it by hand can be tedious but eventually you'll succeed.

      --
      Knowledge is power. Knowledge shared is power lost.
    63. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      That ip address regexp is incomplete, even for ipv4 addresses. Of course, it doesn't even begin to address ipv6.

    64. Re:IP and Hardware addresses by bugs2squash · · Score: 1

      can it solve the problem my 401k has with stocks ?

      --
      Nullius in verba
    65. Re:IP and Hardware addresses by mustafap · · Score: 0, Offtopic

      Is your UID for sale by any chance?

      --
      Open Source Drum Kit, LPLC deve board - mjhdesigns.com
    66. Re:IP and Hardware addresses by Liath · · Score: 1

      About a month ago, we had a "rare treat" (photo)

    67. Re:IP and Hardware addresses by andrikos · · Score: 1

      That's a rather personal question Sir!

    68. Re:IP and Hardware addresses by Domini · · Score: 1

      Watch all the low uids come out of the woodwork now...

    69. Re:IP and Hardware addresses by Maniac-X · · Score: 1

      another one for ips: /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/

      remove single-line C-style comments:
      s;//.*$;;
      s/\/\*.*\*\///g

      multi-line C-style comments:
      /\/\*/,/\*\// { d }

      --
      (A)bort, (R)etry, (I)gnore?_
    70. Re:IP and Hardware addresses by Vadim+Grinshpun · · Score: 4, Funny

      Hmmm... until recently I didn't even realize that low ID's were in vogue :)

    71. Re:IP and Hardware addresses by Requiem18th · · Score: 1

      The heeeeeeeeeeell?

      --
      But... the future refused to change.
    72. Re:IP and Hardware addresses by JerRocks · · Score: 1

      You say that like it's a bad thing...

    73. Re:IP and Hardware addresses by jridley · · Score: 1

      2130706433 is equivalent to 127.0.0.1 too.

    74. Re:IP and Hardware addresses by redJag · · Score: 1

      Whether it cures or causes insomnia depends on your point of view I suppose.

    75. Re:IP and Hardware addresses by Draco · · Score: 1

      What? There's a market for that?

    76. Re:IP and Hardware addresses by kakris · · Score: 1

      That one's easy:

      /(a)pple/;

    77. Re:IP and Hardware addresses by jbezorg · · Score: 1

      That caveat is why the place I work for now does not allow source files from outside the network. Your security and system maintenance then depends on them.

      --
      I've lost all my marbles except one & It's fun to test angular & centripetal acceleration in my skull
    78. Re:IP and Hardware addresses by morcego · · Score: 1

      No.
      0.0.0.0 with netmask 0.0.0.0 means all addresses. (Choose your notation: 0.0.0.0/0, 0.0.0.0/0.0.0.0 etc).

      0.0.0.0 is just 1 address.

      --
      morcego
    79. Re:IP and Hardware addresses by alta · · Score: 1

      32... I wish I would have created an account when they started. At the time, this was just another website that wanted a username... I wasn't interested. Oh well.

      --
      Do not meddle in the affairs of sysadmins, for they are subtle, and quick to anger.
    80. Re:IP and Hardware addresses by jez9999 · · Score: 2, Funny

      You think THAT'S cool? I've seen CMDRTACO posting! Now that was a sight to behold. Actually I think I even saw a -1 once, which was his mom.

    81. Re:IP and Hardware addresses by Mordac · · Score: 1

      Get off my lawn you whippersnapper!

      and while you're at, can you make this a bit more efficient.

      Tried to post my own regexp, but the post filter didn't like it, too many "junk" characters. I knew I wasn't good at regexp, but how dare this system call it junk.

    82. Re:IP and Hardware addresses by jez9999 · · Score: 1

      easier: /(a)/

    83. 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

    84. Re:IP and Hardware addresses by Poltras · · Score: 1

      Close but no cake (I hate smoking). 0.0.0.0 will always be the base network address for whatever netmask you chose, so 0.0.0.0/X will always be the network (collection of address) 0.0.0.0. It is invalid to give yourself that address (although 0.0.0.1 would be valid) and no router in the world will route your traffic. Same is true for all network address (X.Y.0.0/16 for example).

    85. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      easier: /(a)/

      Nope. He wants the "a" from an "apple" only, not from "apples" nor from "anuses".
      /^(a)pple$/ if you want it case-sensitive,
      /^([Aa])pple$/ if you want to allow a capital letter at the beginning,
      /^(a)pple$/i for case-insensitive clods.

    86. Re:IP and Hardware addresses by binner1 · · Score: 1

      While I don't consider my UID low, I do remember signal11. Man he/she was annoying! What about Jon Katz..? Ok, I've obvioulsy been hanging around for far too long.

      -Ben

    87. Re:IP and Hardware addresses by Erich · · Score: 1

      Well, you got me beat.

      --

      -- Erich

      Slashdot reader since 1997

    88. Re:IP and Hardware addresses by bytesex · · Score: 1

      Isn't the plural of 'one anus' 'two ani' ? I jest, I jest..

      --
      Religion is what happens when nature strikes and groupthink goes wrong.
    89. Re:IP and Hardware addresses by corsec67 · · Score: 1

      (Or 127.1, but which is equally, and surprisingly, valid.)

      Dude, that is awesome. Considering how often I use localhost, I can't believe I haven't noticed that until you mentioned it.

      --
      If I have nothing to hide, don't search me
    90. Re:IP and Hardware addresses by darthdrinker · · Score: 1
      Here's one that really does check if it's a valid IP:

      ^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])
      \.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)
      \.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)
      \.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$

      If you think, hey that darthdrinker is one smart cookie, than that's cute but it's wrong!

    91. Re:IP and Hardware addresses by ElectricRook · · Score: 1

      Much appreciated by those who appreciate such things...

      --
      - High Tech workers, please say NO to Union Carpenters, their Union sees fit to control our compensation.
    92. Re:IP and Hardware addresses by alta · · Score: 1

      First you should wget -r http://slashdot.org/ Then regex the result, let me know how that works out.

      --
      Do not meddle in the affairs of sysadmins, for they are subtle, and quick to anger.
    93. Re:IP and Hardware addresses by adonoman · · Score: 1

      Or try it as a single number: 2130706433

    94. Re:IP and Hardware addresses by Twigmon · · Score: 1

      Oh wow. That code is not even funny. It made me sad. Please don't post such terrible things where others are likely to see them :-/

    95. Re:IP and Hardware addresses by Chunky+Kibbles · · Score: 1

      That mac address one seems like a lot of work. Why not /^[0-9A-F]{2}(:[0-9A-F]{2}){5}$/i

      Or [0-9a-fA-F] as you had, if your regex system doesn't understand case insensitivity.

      Gary (-;

    96. Re:IP and Hardware addresses by jgrahn · · Score: 1

      It seems both Opera and ping in Windows interpret individual parts with leading zeros as octal. More interestingly, Opera also accepts hexadecimal.

      There's a whole list of documented valid formats in the inet_addr(3) man page on my system -- including just a 32-bit decimal number. There are probably others too, in other implementations.

      That makes constructing a regexp that validates any arbitrary IP address, and not just a valid dot-decimal, a bit more cumbersome.

      And often pointless. Just try to use the string, and accept the possibility of error ...

    97. Re:IP and Hardware addresses by fj3k · · Score: 1

      That assumes that the only thing present is the apple. If we need to find the apple and then extract the 'a', you would do something like this:
      /\b(a)pple\b/i

      --
      Two men claimed to have walked into a bar. Only one had the bruises to prove it.
    98. Re:IP and Hardware addresses by Predius · · Score: 1

      That's not 100% true... the network addy is normally considered tabu for device assignment because in the past, it was also the broadcast address. Nowadays with an all 1's broadcast it's the last ip in a range that's 'off limits' leaving the network addy viable. If you assign it as a /32 for a point to point connection, it's also valid. (I manage a few dialup pools, and have had to explain to more than one person that x.x.x.0 is in fact, 100% usable.)

    99. Re:IP and Hardware addresses by mikiN · · Score: 4, Funny

      You must be new h... (looks at PP's ID, gasps)
      Nevermind.

      --
      The Hacker's Guide To The Kernel: Don't panic()!
    100. Re:IP and Hardware addresses by Kymermosst · · Score: 3, Funny

      So, would anyone like to buy my new T-shirt, it says "There is no place like 2130706433."

      --
      "Alcohol, Tobacco, Firearms, and Explosives" should be a convenience store, not a government agency.
    101. Re:IP and Hardware addresses by DMUTPeregrine · · Score: 1

      I'm in the same boat. It makes me sad sometimes. One extra digit makes all the difference.

      --
      Not a sentence!
    102. Re:IP and Hardware addresses by david.given · · Score: 1
      Bizarrely, I don't use it either --- and I do know about it. My internal network's on 10.0.0.0/8, too, which means I can use 10.1, 10.2 etc to refer to my machines.

      I probably need more coffee. Or possibly less coffee.

    103. Re:IP and Hardware addresses by zummit · · Score: 1

      http://sexyregex.com/ - Regex online test, examples, documentation

    104. Re:IP and Hardware addresses by pez · · Score: 1

      Nope, I didn't buy it. I've been reading CmdrTaco since this site was called "Chips & Dips."

    105. Re:IP and Hardware addresses by slashnik · · Score: 1

      One of our operators once decided to tidy up the hosts file on the openview sun box
      Added leading zeros to all the octets in all IP addreses.
      That created a fair few problems.

    106. Re:IP and Hardware addresses by PAjamian · · Score: 1

      So if I get this right, 0.0.0.0 is a valid ip address?

      It can be depending on context. If you open a socket with 0.0.0.0 passed as the listening IP address then it generally means that the socket will listen on all IPs that are configured for the computer.

      --
      Windows is a bonfire, Linux is the sun. Linux only looks smaller if you lack perspective.
    107. Re:IP and Hardware addresses by Architect_sasyr · · Score: 1

      They might have the "last time logged in" column in their db. Dunno why they don't take out those who haven't logged in x (say 5) years and give people (say me) those accounts. I was reading slashdot back then, damnit. Was just too lazy to create an account...

      So was I but I'll be damned if it doesn't sound like we're just whining about it!
       
      On topic, here's one for you:

      (
        (
          # Handle http, https, and relative URIs:
          ((https?://([A-Za-z0-9][A-Za-z0-9\-]*(\.[A-Za-z0-9][A-Za-z0-9\-]*)*\.?))|
              ([A-Za-z0-9\-\_\.\!\~\*\'\(\)]|(%[2-9A-Fa-f][0-9a-fA-F]))+)?
          ((/([A-Za-z0-9\-\_\.\!\~\*\'\(\)]|(%[2-9A-Fa-f][0-9a-fA-F]))+)*/?) # path
            (\#([A-Za-z0-9\-\_\.\!\~\*\'\(\)\+]|(%[2-9A-Fa-f][0-9a-fA-F]))+)? # fragment
          )|
        # Handle ftp:
        (ftp://([A-Za-z0-9][A-Za-z0-9\-]*(\.[A-Za-z0-9][A-Za-z0-9\-]*)*\.?)
          ((/([A-Za-z0-9\-\_\.\!\~\*\'\(\)]|(%[2-9A-Fa-f][0-9a-fA-F]))+)*/?) # path
          (\#([A-Za-z0-9\-\_\.\!\~\*\'\(\)\+]|(%[2-9A-Fa-f][0-9a-fA-F]))+)? # fragment
          )
        )

      It's not mine though, comes courtesy dwheeler.com (whom I've been chatting to recently, hence remembering it was online). A "mostly safe" validation of URI's.

      --
      Me failed English...
      FreeBSD over Linux. If my comments seem odd, this may explain...
    108. Re:IP and Hardware addresses by Timex · · Score: 1

      You've only been around "too long" when you remember that it was a big deal to be able to NOT get posts pertaining to him... ;)

      --
      When politicians are involved, everyone loses.
    109. Re:IP and Hardware addresses by Tet · · Score: 1

      Is your UID for sale by any chance?

      How much are you offering?

      --
      "The invisible and the non-existent look very much alike." -- Delos B. McKown
    110. Re:IP and Hardware addresses by MadCat · · Score: 1

      Must mean I'm older than dirt then :P

      --
      There is no sig...
    111. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      I would do anything to look like Gandalf.

    112. Re:IP and Hardware addresses by phantomfive · · Score: 1

      I was doing something similar the other day, and it might be worth mentioning your regular expression for comments misses comments that are inside of quotation marks. You might not care, though.

      --
      Qxe4
    113. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      ...if you use Windows.

    114. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      Yes?

    115. Re:IP and Hardware addresses by Daengbo · · Score: 0, Offtopic

      Getting modded down consistently ruins your Karma (I pine for the days when Karma was still a number and could be lauded over others ... hehe)

      I miss the number. It was like having a "Geekiness" stat on your D&D character.

      Note: This is my second account for the same reason that others have mentioned.

    116. Re:IP and Hardware addresses by Daengbo · · Score: 1

      You have to remember when they were on sale before the first dot-bomb, right? Quite expensive, too, if I remember. Or didn't you buy yours?

    117. Re:IP and Hardware addresses by TheoMurpse · · Score: 1

      like a god damn fountain of aol youth.

      Me too!!!!!

    118. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      Fuck Adobe and their bloated plugins and disrespect for previously used names.

    119. Re:IP and Hardware addresses by amorsen · · Score: 1

      Classful adressing died at least ten years ago. Please don't bring it back. 192.168.0.0/16 is a perfectly valid network, and 192.168.4.255 is a perfectly valid IP address. Windows XP won't work with it, but that is just another reason why Windows should not be allowed to connect to IP networks.

      --
      Finally! A year of moderation! Ready for 2019?
    120. Re:IP and Hardware addresses by amorsen · · Score: 1

      Surely 010.000.000.001 is still valid?

      Sure it is, if you are trying to reach 8.0.0.1. IPv4 addresses should only be shown or accepted as input in canonical non-shortened format.

      Then there's 10.5/16, which netfilter accepts as 10.5.0.0/16 (unless the recent flame war made them change it), but according to the standards that means 10.0.0.5/16 and is therefore not a valid network. Just say no.

      --
      Finally! A year of moderation! Ready for 2019?
    121. Re:IP and Hardware addresses by amorsen · · Score: 1

      despite them being uncommon 001.001.001.001 should still read as valid

      How many people except 010.000.000.001 to take them to 8.0.0.1? Of those people, how many use that feature as more than a curiousity?

      --
      Finally! A year of moderation! Ready for 2019?
    122. Re:IP and Hardware addresses by Poltras · · Score: 1

      Classful adressing died at least ten years ago. Please don't bring it back. 192.168.0.0/16 is a perfectly valid network, and 192.168.4.255 is a perfectly valid IP address. Windows XP won't work with it, but that is just another reason why Windows should not be allowed to connect to IP networks.

      I didn't say otherwise. In fact, I support your point. Based on the sole address 192.168.4.255, we can only say it's a valid IP address without further knowledge of its netmask. I never said to bring back classes, just that we can use them to define a first valid netmask during validation.

      And I don't know about XP. Will it really choke with a valid netmask and a .255 address? Never tried to, TBH.

    123. Re:IP and Hardware addresses by amorsen · · Score: 1

      I never said to bring back classes, just that we can use them to define a first valid netmask during validation.

      And I am saying you can't use classes for anything. They are gone.

      --
      Finally! A year of moderation! Ready for 2019?
    124. Re:IP and Hardware addresses by Vadim+Grinshpun · · Score: 1

      I was actually not kidding :) I was blissfully unaware until it was mentioned to me in a conversation a year or so back.
      As for where I got mine--well, I registered in '98 or '99 :)

    125. Re:IP and Hardware addresses by illuvata · · Score: 1

      This is quite neat, but can I tell it to use a more advanced kind of RegExp?

    126. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      a/s/l

    127. Re:IP and Hardware addresses by colmore · · Score: 1

      I registered in 1999 and mine isn't that low. So it was probably '98.

      --
      In Capitalist America, bank robs you!
    128. Re:IP and Hardware addresses by colmore · · Score: 1

      anus that pluralizes to ani is "ring" and is what you're thinking.

      anus that pluralizes to anus is "old woman" which is kind of funny.

      latin!

      --
      In Capitalist America, bank robs you!
    129. Re:IP and Hardware addresses by colmore · · Score: 1

      Seems like you could get in on the rush being born in the late 70s or early 80s. A low ID means you're probably not in generation facebook, but not much more than that.

      --
      In Capitalist America, bank robs you!
    130. Re:IP and Hardware addresses by ConceptJunkie · · Score: 1

      I think the "no max" was before my time, but signal11 sounds familiar.

      --
      You are in a maze of twisty little passages, all alike.
    131. Re:IP and Hardware addresses by drunkennewfiemidget · · Score: 1

      Yes.

    132. Re:IP and Hardware addresses by morgan_greywolf · · Score: 2, Funny

      I'd rather take issue with the fact it completely fails on IPv6 addresses.

      Wake me up when that actually matters, k?

    133. Re:IP and Hardware addresses by Xel'Naga · · Score: 1
      I feel your pain.
      Say you want to search a logfile for non-zero errors. Easy: "error=[^0]". Now assume that you want to search for "error=" that do not end on one of "handled", "expected", "not found". This is impossible*, and if you want to google why, you need to know that it is called a negative look-ahead assertion.

      *As you can see in the link, I had a problem, and thought "I'll use regular expression". Then I had two solvable problems that needed regular expressions.

    134. Re:IP and Hardware addresses by Patik · · Score: 1

      Actually, CmdrTaco is 1, so his mom would be 0 (-1 would be his grandmother). Bonus points if you can guess his father's or sibling's.

    135. Re:IP and Hardware addresses by painehope · · Score: 1

      The module looks cool...but I can't help but be amused by adding regex capabilities to PERL fer chrissakes! The language already has awesome regex capabilities (yes, they can be a bit arcane...but, hell, that's half the fun of perl - obfuscation if you want it) and was designed with parsing data in mind.

      --
      PC moderators can suck my White pierced, tattooed dick. If you think pride == hate, s/dick/Aryan meat mallet/g.
    136. Re:IP and Hardware addresses by RichiH · · Score: 1

      richih@adamantium ~ % perl -e 'if ('112.112.112.112' =~ /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.|$)){4}/) {print "yes"} else {print "no"}' no% richih@adamantium ~ % perl -e 'if ('112.112.112.112' =~ /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.|$)){4}(?!\.)/) {print "yes"} else {print "no"}' no% richih@adamantium ~ %

    137. Re:IP and Hardware addresses by RichiH · · Score: 1

      Fail after /. ate my reply ID and I had to repost. Correct version is:

      richih@adamantium ~ % perl -e 'if ('112.112.112.112' =~ /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.|$)){4}/) {print "yes"} else {print "no"}'
      no%
      richih@adamantium ~ % perl -e 'if ('112.112.112.112' =~ /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.|$)){4}(?!\.)/) {print "yes"} else {print "no"}'
      no%
      richih@adamantium ~ %

    138. Re:IP and Hardware addresses by UnderCoverPenguin · · Score: 1

      So if I get this right, 0.0.0.0 is a valid ip address?

      In the context of obtaining an IP address from a DHCP (or BOOTP) server, the sender will use 0.0.0.0 as the source address.

      --
      Don't try to out wierd me, three-eyes. I get stranger things than you, free with my breakfast cereal. --Zaphod Beeblebr
    139. Re:IP and Hardware addresses by Anonymous Coward · · Score: 0

      0.0.0.0 is a route reference, not an address.

    140. Re:IP and Hardware addresses by omfgnosis · · Score: 1

      I wonder where 299.299.299.299 goes.

  2. Here's One for Slashdot Stories! by Anonymous Coward · · Score: 4, Funny

    (Useful) Stupid * Tricks

    Yes sir, that will guarantee a front page story. You better head back to the drawing board if it doesn't fit that pattern. Next week: (Useful) Stupid Starcraft Tricks.

    1. Re:Here's One for Slashdot Stories! by Malevolent+Tester · · Score: 4, Funny

      Next week: (Useful) Stupid Starcraft Tricks.

      You can assign a building, building add-on, or a group of up to 12 units to a single key. To do this, select what you want to assign, then hold down Control and select a number on the keyboard between 0-9. Then, when you want to select what you assigned, simply press the number of the group that you want. Pressing a group number twice will center the screen on the group.

      --
      If you haven't made a developer cry, you've wasted a day.
    2. Re:Here's One for Slashdot Stories! by McWilde · · Score: 4, Funny

      That doesn't look right...
      Try:

      /^\(Useful\) Stupid \w+ Tricks$/

      Also, I noticed that the previous stupid tricks stories ended with a question mark, but this one doesn't. So:

      /^\(Useful\) Stupid \w+ Tricks\??$/

      --
      Maybe
    3. Re:Here's One for Slashdot Stories! by LordKronos · · Score: 1

      That won't do what you want in most regex flavors. What you want (at least in perl) is something more like:
      \(Useful\) Stupid .+ Tricks

      (feel free to wrap it in // or /^$/ if you like)

    4. Re:Here's One for Slashdot Stories! by kestasjk · · Score: 1

      \(Useful\) Stupid [^ ]+ Tricks

      --
      // MD_Update(&m,buf,j);
    5. Re:Here's One for Slashdot Stories! by Talderas · · Score: 2, Interesting

      You can permanently cloak zerg units that can burrow if you control an arbiter. By burrowing the zerg unit just as it enters the arbiter's cloaking field radius, the zerg will become permanently cloaked.

      --
      "Lack of speed can be overcome. In the worst case by patience." --Znork
    6. Re:Here's One for Slashdot Stories! by dayton967 · · Score: 1

      Shouldn't it have been Stupid .+ Tricks. Though Next week it will be stupid human tricks.

    7. Re:Here's One for Slashdot Stories! by Anonymous Coward · · Score: 0

      show me the money

    8. Re:Here's One for Slashdot Stories! by Poltras · · Score: 1

      Next week: (Useful) Stupid Starcraft Tricks.

      m4d 5k1llz!!!!!!!!!

    9. Re:Here's One for Slashdot Stories! by Wizworm · · Score: 1
      don't you mean

      \(Useful\) Stupid \S+ Tricks

      \s == whitespace character
      \S == non Whitespace character

      --
      I always thought of Creationism as the Raving Right's version of the Loony Left's Anthropogenic Global Warming-brightmal
    10. Re:Here's One for Slashdot Stories! by meta+coder · · Score: 1

      (Useful) Stupid * Tricks Yes sir, that will guarantee a front page story

      Extracted from (useful) stupid slashdot tricks

    11. Re:Here's One for Slashdot Stories! by Komi · · Score: 1
      I thought you were going to suggest

      (Useful) Stupid Glob-pattern Tricks

      --
      The ultimate goal of science is to unify all forces of nature to a single law that can be silk-screened onto a T-shirt.
    12. Re:Here's One for Slashdot Stories! by __aawkdb2598 · · Score: 1
      • When playing as Zerg, don't use "select larva" from 4 hatcheries to build 12 units. Just double-click on a larva. It's much faster.
      • If you're playing against a computer and don't feel like exerting yourself at all, take a probe out early-game and tease their workers. The stupid AI will send all of its workers chasing you all around the map.
      • When playing on Battlenet, remember that no one there has really played only 25 games. The game's been out for a decade.
    13. Re:Here's One for Slashdot Stories! by Anpheus · · Score: 2, Funny

      Did you get that from the tips that pop up every game when you first install StarCraft?

    14. Re:Here's One for Slashdot Stories! by Anonymous Coward · · Score: 0

      Shouldn't it have been Stupid .+ Tricks. Though Next week it will be stupid human tricks.

      You know, if you submit a story to slashdot of the form /^\(Useful\) Stupid \w+ Tricks$/ then you are sure to have it posted to the main page!

    15. Re:Here's One for Slashdot Stories! by Domini · · Score: 1

      1. Select all hives
      2. Ctrl-0
      3. 0-s-h
      4. goto 3.

    16. Re:Here's One for Slashdot Stories! by mhall119 · · Score: 1

      Neither of you are taking into account multi-word stupid trick topics.

      --
      http://www.mhall119.com
    17. Re:Here's One for Slashdot Stories! by Eponymous+Bastard · · Score: 1

      You can confuse your enemies with an uncloaked observer or dark templar:

      Just have one of your high templars create an illusion and send them over to your opponent's base. The illusion is not cloaked.

    18. Re:Here's One for Slashdot Stories! by Eponymous+Bastard · · Score: 2, Interesting

      On fastest:

      Stasis your oponents' fleet with your arbiter and start a 15 second countdown. On zero, your teammate nukes the stasis. Wait 30 seconds for the nuke to come down right on your open-mouthed oponents' fleet.

      To add insult to injury, if you manage to stasis both their and your ships, you can recall them out right before the nuke hits.

    19. Re:Here's One for Slashdot Stories! by Anonymous Coward · · Score: 0

      Isn't that more a hack than a trick? I always thought tricks were cleaver ways to use documented features, while this is more of a way to take advantage of a bug.

    20. Re:Here's One for Slashdot Stories! by JThundley · · Score: 1

      You can group mutalisks together more tightly by having an overlord in the group with them. Without the overlord they tend to spread out quickly whenever they sit still. When they are packed together tightly their attack will bounce in the same direction together, unleashing a powerful 3-kill shot.

    21. Re:Here's One for Slashdot Stories! by Anonymous Coward · · Score: 0

      (Useful) Stupid .* Tricks

      There, fixed that for ya.

    22. Re:Here's One for Slashdot Stories! by Sentry21 · · Score: 1

      Your post would be more apropos in a '(Useful) Stupid Slashdot Tricks' thread.

    23. Re:Here's One for Slashdot Stories! by garaged · · Score: 1

      The best trick I know about stracraft is to use protos, it's quite impossible to beat a good protos player

      --
      I'm positive, don't belive me look at my karma
    24. Re:Here's One for Slashdot Stories! by Phroggy · · Score: 1

      Since you can only assign 12 units to a group, if you want to control a larger group do this:

      1) Divide your army into 12-unit subgroups plus one "leader" per subgroup

      2) Assign all of the leaders to a numbered group

      3) Select each subgroup (not including their leader) and assign them to a numbered group, then right-click the group leader to make them follow that leader

      4) Send the group of leaders into battle; the army will follow.

      --
      $x='S24;r)>63/* h@<5+oZ)32"5cz';$me='phroggy'x$];
      $x=~y+ -xz+\0-Tx+;print$_^chop$me for split'',$x;
    25. Re:Here's One for Slashdot Stories! by Anonymous Coward · · Score: 0

      I've written a regexp that parses context-free grammars!! Ain't perl great?????

  3. New Slashot Section by Frankie70 · · Score: 5, Interesting

    Maybe we should have a new section for "Useful Stupid Tricks" on Slashdot.

    1. Re:New Slashot Section by iammani · · Score: 0, Troll

      Maybe we should have a new section for "Useful Stupid Tricks" on Slashdot.

      ...So that i can block "Stupid Tricks" posts from appearing on my slashdot home page

    2. Re:New Slashot Section by Anonymous Coward · · Score: 0

      Maybe we should have a new section for "Useful Stupid Tricks" on Slashdot.

      +1

      Please restore the universe's balance or at least kill the idle section thing.

    3. Re:New Slashot Section by VitaminB52 · · Score: 1
      Next installment: "Useful Stupid Manager Tricks"
      • If it's understandable by a manager, than it's stupid...
      • and if it gives you your much deserved salary hike, than it's useful
    4. Re:New Slashot Section by timelorde · · Score: 1


      Only a Guardian has that much power.

    5. Re:New Slashot Section by Gazzonyx · · Score: 1

      Actually, I've been enjoying this stupid tricks series. Especially since I know I can always google these later when I've got a regex problem... it's one of those things that I look up every time I have to use them and then promptly forget the next day.

      --

      If I mod you up, it doesn't necessarily mean I agree with what you've said, sorry.

    6. Re:New Slashot Section by Anonymous Coward · · Score: 0

      If you can't distinguish "than" from "then", then you're stupid.

    7. Re:New Slashot Section by Anonymous Coward · · Score: 0

      Maybe we should have a new section for "Useful Stupid Tricks" on Slashdot.

      ...So that i can block "Stupid Tricks" posts from appearing on my slashdot home page

      +1

  4. End it all... by Notquitecajun · · Score: 1, Funny

    format c:*.*

    1. Re:End it all... by SatanicPuppy · · Score: 1

      That didn't do anything...All I got was:

      tcl>format C:*.*
      C:*.*

      --
      ad logicam Claiming a proposition is false because it was presented as the conclusion of a fallacious argument.
    2. Re:End it all... by tscheez · · Score: 1

      Invalid drive specification.

      --
      Supplies!
    3. Re:End it all... by Tatsh · · Score: 1

      bash: format: command not found

    4. Re:End it all... by dreamsofcaffeine · · Score: 1

      Try del /F /S /Q *. And you'll be happy.

    5. Re:End it all... by slashbart · · Score: 1
      I tried it, nothing interesting happened. Would you explain please :-)

      ubu8:~$ del /F /S /Q *.
      bash: del: command not found

  5. ARGH!!!! by soapdog · · Score: 2, Funny

    You see yourself in digg.com. You are likely to be eaten by a grue.

    --
    -- Por mais que eu ande no vale das trevas e da morte, meu PowerMac G4 Não Travará!!!
    1. Re:ARGH!!!! by Anonymous Coward · · Score: 2, Insightful

      So clearly, Slashdot's shit never stank?

      No, seriously, why the bitching? Did you expect the site to just keep reporting dry stories about incremental Linux kernel upgrades for its entire existence? You expected a website to never change and never update with the times? Just because it's old doesn't mean it's sacred.

    2. Re:ARGH!!!! by Anonymous Coward · · Score: 0

      Yes it so does.

    3. Re:ARGH!!!! by Bandman · · Score: 1

      new doesn't necessarily mean good.

      If you like good, and old was good, but new is bad, guess which one you'd rather go with?

      I think slashdot is usable, but it gets in my way a lot, and I find myself doing things to make it usable rather than being pleased by enhanced features.

  6. Regex Support by Extremus · · Score: 2, Interesting

    I have used regex in the past, mainly for keeping long SQL scripts. The problem is the lack of full support for regex in most of editors. IMO the best (for windows, at least) is the EditPad Pro.

    1. Re:Regex Support by not+already+in+use · · Score: 1

      Check out jEdit, and it's cross-platform.

      --
      Similes are like metaphors
    2. Re:Regex Support by jonadab · · Score: 2, Informative

      Emacs has pretty good regex support, and is available for Windows. HTH.HAND.

      --
      Cut that out, or I will ship you to Norilsk in a box.
    3. Re:Regex Support by bunratty · · Score: 1

      I used to use jEdit. It's a decent text editor, but now I use Notepad++. I'm also trying Komodo Edit as a possible replacement to Notepad++. All three editors are open source, but Notepad++ is Windows only.

      --
      What a fool believes, he sees, no wise man has the power to reason away.
    4. Re:Regex Support by partenon · · Score: 1

      Most editors? Windows ones, right?

      To parse logs, the best set of tools one have is awk+grep+sed

      No kidding, with these tools, you can rule the world if you are willing to spend some time learning awk and regexp's properly.

      --
      ilex paraguariensis for all
    5. Re:Regex Support by Machtyn · · Score: 1

      Note that the person who makes EditPad also runs the website http://www.regular-expressions.info/, which happens to be a very useful tutorial and educational regexp site.

    6. Re:Regex Support by DrVxD · · Score: 1

      The problem is the lack of full support for regex in most of editors

      vi supports regexps; emacs supports regexps. If it's not on that list (or a descendant thereof), it's not an editor...

      --
      Not everything that can be measured matters; Not everything that matters can be measured.
    7. Re:Regex Support by Tatsh · · Score: 1

      Notepad++

      If you love Kate like me, Kate is available as beta for Windows. Beyond that, there is gVim/Vim and many other editors ported from Unix-like environments.

    8. Re:Regex Support by Manfre · · Score: 1

      Ultraedit has really good regex support

  7. How about by cbiltcliffe · · Score: 3, Funny

    Stupid (Useful) Ask Slashdot tricks?

    I'm not sure whether these are legitimate, or just a "I don't know what the hell I'm doing, so let's see if I can get someone else to show me how to do my job, under the guise of sharing information."

    I'd like to say the former, but my cynicism is making me lean to the latter.....

    --
    "City hall" in German is "Rathaus" Kinda explains a few things......
    1. Re:How about by Anonymous Coward · · Score: 5, Interesting

      I actually like these. Nice little highly enriched concentrations of geekery on a single page. Think how long it might take to round up the sort of stuff that appears here by Googling.

      Turing word: insipid
      In a sentence: You find this page insipid but I find it inspiring.

    2. Re:How about by cbiltcliffe · · Score: 1

      You're probably right. And there are certainly some useful nuggets on these pages, but I wouldn't be Googling for regex's anyway. That's the kind of stuff I'd write from scratch, because I want to be sure of what it does.

      Maybe I'm weird that way, but I don't often take complex programming code and just copy/paste from Google. I don't trust the Internet that much.

      Although that could be because I'm also a musician, and Googling lyrics/chords, etc for songs inevitably leads to some stuff where you think "Was this guy listening to the same song I am?"

      --
      "City hall" in German is "Rathaus" Kinda explains a few things......
    3. Re:How about by ohxten · · Score: 1

      Lighten up, I find the comments for these "(Useful) Stupid * Tricks" stories to be very entertaining.

      --
      Need an automatic screenshot taker? Try here.
    4. Re:How about by interiot · · Score: 1

      Nobody is posting tech-support or homework style questions. It's just generally educational material. It's true you could get more/better information from the relevant O'Reilly book, but sometimes it's good to see the treadworn solutions from the hoi polloi as well.

    5. Re:How about by Bandman · · Score: 4, Interesting

      I like it, but I've got a bookmark folder called "Slash-doc" where I store useful threads that contain a lot of information.

      I've got a lot of threads bookmarked.

      Best Practices for Process Documentation

      How would you make a distributed Office system

      Quality Open Source / Calendar / Messaging Systems

      and some others.

      Some of the information in the threads is out of date, but the ideas are useful and interesting to read. I need to go back through Ask Slashdot and get the more recent threads that seem to act as references

    6. Re:How about by Bandman · · Score: 1

      Yea, I stopped trying code I didn't understand the first time I tried the bash fork bomb

    7. Re:How about by lysergic.acid · · Score: 1

      what does it matter so long as the question initiates an engaging discussion? this may come as a shock to you, but for a lot of geeks, their work and personal interests are not mutually exclusive. so a discussion topic that they are genuinely interested in may also help them do their job.

      if you're so pettily concerned that your information might be helpful to others (or you might be doing someone else's work), then don't participate in a /. discussion. it seems like whenever there's any kind of discussion on practical matters there's always some loser complaining that useful information is being freely disseminated or looking for ulterior motives in the question submitter.

      and it's much easier to do a quick google search if you're actually looking for a specific regexp rather than submitting such a broad ask /. question hoping that your question will be picked up, and that by chance someone will post the exact solution that you need. so i'd say your suspicions are more rooted in irrational paranoia than cynicism.

    8. Re:How about by Anonymous Coward · · Score: 0

      Any instance of 'regex' or 'regular expression' within 10 words of 'geek' or 'nerd':

      ((?:(?:[Rr]egex|[Rr]egular [Ee]xpression)(?:(?:\W+\w*)?\s+\S+){0,10}\s*(?:[Nn]erd|[Gg]eek))|(?:(?:[Nn]erd|[Gg]eek)(?:(?:\W+\w*)?\s+\S+){0,10}\s*(?:[Rr]egex|[Rr]egular [Ee]xpression)))

    9. Re:How about by cbiltcliffe · · Score: 1

      Oh, I didn't say anything negative about the comments. I agree. Sometimes they're downright hilarious.

      It's the whole point of the article itself that I'm questioning.

      --
      "City hall" in German is "Rathaus" Kinda explains a few things......
    10. Re:How about by cbiltcliffe · · Score: 1

      Hey! I am irrationally paranoid!!

      Actually, I definitely am paranoid, but maybe "irrationally" is going a little far. But that's what's caused me to have never been infected with a computer virus, never get hacked, and to not run antivirus software or have a security system in my house.

      You're probably right, though, but it just seems there's been a heck of a lot of these lately.

      --
      "City hall" in German is "Rathaus" Kinda explains a few things......
    11. Re:How about by Zadaz · · Score: 1

      Nice little highly enriched concentrations of geekery on a single page.

      I admire your optimism about Slashdot's signal to noise ratio in the absence of all evidence to the contrary.

    12. Re:How about by moreati · · Score: 1

      Excellent, could you post the rest of the collection? I've been looking for 'best of ask slashdot' list for a while, mine is very incomplete.

      Thanks, Alex

    13. Re:How about by poached · · Score: 1

      that's cool and all, but should slashdot's search suck less so you can just find what you need?

      For example, I searched for (Useful) in the search box and got zero hits. How? I don't know. But these threads are as good as useless if I can't find them again.

      (yes I know google will find it, but it's seems like I shouldn't need to rely on that).

    14. Re:How about by Bandman · · Score: 1

      Sure. I keep them on my work computer, so I'll have to check when I get a chance there. Thanks for digging the idea.

    15. Re:How about by gr8dude · · Score: 1
      I have such a list too
  8. 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
    1. Re:Regexp-based address validation by Anonymous Coward · · Score: 0

      I'm curious, could someone explain exactly what that is about? It says it's to validate if something is a valid email adress. What makes it so complex? Should it only validate if there's an @ sign and then a . to have it be a valid email address? What more complex cases are there?

    2. Re:Regexp-based address validation by maxume · · Score: 1

      Can a valid address contain more than one @ character?

      If it can't, that regex, in my opinion, is really stupid, as it would be much clearer to deal with the part of the address before the @ separately from the part after the @.

      --
      Nerd rage is the funniest rage.
    3. Re:Regexp-based address validation by neoform · · Score: 4, Funny

      Best part of that Regex? It's easy to modify too!

      --
      MABASPLOOM!
    4. Re:Regexp-based address validation by Daas · · 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.

    5. Re:Regexp-based address validation by Anonymous Coward · · Score: 0

      It's been some time since I tested it but that regex fails on many addresses (both valid and invalid).

      The best regex tip of them all: Never use a regex in situations that require a parser!

    6. Re:Regexp-based address validation by maxume · · Score: 1

      Well, that makes more sense. It still seems a bit saner to break it up, but I don't think in regular expressions, I think in English and slowly test my way to something that works for what I am doing. It is a good thing I am not responsible for writing anything important, just this or that for my own use.

      --
      Nerd rage is the funniest rage.
    7. Re:Regexp-based address validation by feldicus · · Score: 1, Troll

      There's no such thing as a beautiful regexp.

      feldicus

    8. Re:Regexp-based address validation by Thaelon · · Score: 1

      Beautiful? That thing is hideous.

      And largely worthless, since many email systems won't allow you to use all the possibilities that RFC 822 actually allows. Some won't let you sign up with many characters that are technically legal. Some won't send mail to addresses that contain characters that are technically legal. Not to mention it's specific to Perl.

      --

      Question everything

    9. Re:Regexp-based address validation by Anonymous Coward · · Score: 0

      Hey that looks like one of my friends Perl scripts!

    10. Re:Regexp-based address validation by Bandman · · Score: 1

      only 1 @, arbitrary number of .'s in either direction...

      I don't know if a leading dot is valid. I like I don't know if you could have .com@slashdot.org

      I think it's got to be prepended with an alphanumeric.

    11. Re:Regexp-based address validation by DiegoBravo · · Score: 1

      In reality what it is beautiful is the feeling of not having to write and debug a parser for it in whatever language...

      It also feels beautiful when you see the "Windows Admin" (if that thing can exist) looking for a Visual Basic programmer to resolve that obvious "programming task".

    12. Re:Regexp-based address validation by noidentity · · Score: 2, Funny

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

      And as a bonus, it can be used as a source of random bits, after base-64 decoding.

    13. Re:Regexp-based address validation by xenocide2 · · Score: 4, Insightful

      The regex is beautiful in the sense that it lets you not be one of those assholes who refuses valid email addresses.

      --
      I Browse at +4 Flamebait

      Open Source Sysadmin

    14. Re:Regexp-based address validation by ByteSlicer · · Score: 1

      I find the ones on this page more useful: http://www.regular-expressions.info/email.html

    15. Re:Regexp-based address validation by marcosdumay · · Score: 1

      I can insert comments into a mail address????? How nice! How do I do that?

      (For the ones that didn't see the link, there is a note there saying that the - 2 pages long - regex doesn't work with comments.)

    16. Re:Regexp-based address validation by Abcd1234 · · Score: 1

      Well, you know what they say: when you have a hammer, everything looks like a nail.

      Or: why all programmers should be required to take a class in formal languages.

    17. Re:Regexp-based address validation by truthsearch · · Score: 1

      I often use this simpler regex to validate e-mail addresses during form validation:

      /^[a-z0-9.!#$%&'*+\-\/=?\^_`{|}~]+@[a-z0-9.-]+\.[a-z]{2,4}$/i

    18. Re:Regexp-based address validation by Atreju · · Score: 0

      What about people who use underscore in their domain name? I know it's against the RFC, but I've seen such domains and BIND supports them.

    19. Re:Regexp-based address validation by RomulusNR · · Score: 1

      Yet another wasteful obsessive use of one language to do everything. We wouldn't want to do this with more than one regex and program logic, oh no.

      I'm gonna go write a C++ compiler wholly in PHP now. Not because it makes any sense to do so, but BECAUSE I CAN. And then everyone can see how clever (read: insane) I am.

      --
      Terrorists can attack freedom, but only Congress can destroy it.
    20. Re:Regexp-based address validation by Thaelon · · Score: 1

      What good is an RFC-valid email address that nobody will accept mail from?

      --

      Question everything

    21. Re:Regexp-based address validation by truthsearch · · Score: 1

      I know it's against the RFC

      You just answered your own question.

    22. Re:Regexp-based address validation by NickFitz · · Score: 1

      There's more to it than that, as you'll find if you look at RFC 822 (part 6). For example, you need to check for conformance with the following syntax:

      domain-literal =  "[" *(dtext / quoted-pair) "]"
      atom        =  1*<any CHAR except specials, SPACE and CTLs>
      quoted-pair =  "\" CHAR                     ; may quote any char
      phrase      =  1*word                       ; Sequence of words
      word        =  atom / quoted-string
      address     =  mailbox                      ; one addressee
                  /  group                        ; named list
      group       =  phrase ":" [#mailbox] ";"
      mailbox     =  addr-spec                    ; simple address
                  /  phrase route-addr            ; name & addr-spec
      route-addr  =  "<" [route] addr-spec ">"
      route       =  1#("@" domain) ":"           ; path-relative
      addr-spec   =  local-part "@" domain        ; global address
      local-part  =  word *("." word)             ; uninterpreted
                                                  ; case-preserved
      domain      =  sub-domain *("." sub-domain)
      sub-domain  =  domain-ref / domain-literal
      quoted-string = <"> *(qtext/quoted-pair) <">; Regular qtext or
                                                  ;   quoted chars.
      qtext       =  <any CHAR excepting <">,     ; => may be folded
                      "\" & CR, and including
                      linear-white-space>
      domain-ref  =  atom                         ; symbolic reference

      (... and so on in enormous detail - I've definitely missed a few bits.)

      Email address validation isn't as simple as people think ;-)

      (OT: why does /.'s filter mistake a quote from an RFC for ASCII art, forcing me to post in Code mode?)

      --
      Using HTML in email is like putting sound effects on your phone calls. Just say <strong>no</strong>.
    23. Re:Regexp-based address validation by Luyseyal · · Score: 1

      Which will break when Google and Microsoft finally get their grubby hands on .google and .microsoft. The only thing you should do to validate addresses is require double-entry and send a URL to the email address for confirmation. Anything else is hopelessly subject to failure.

      -l

      --
      Help cure AIDS, cancer, and more. Donate your unused computer time to worldcommunitygrid.org. Join Team Slashdot!
    24. Re:Regexp-based address validation by Luyseyal · · Score: 1

      What good is an RFC-valid email address that nobody will accept mail from?

      Indeed. I bought my .info domain back before .info was spam-capital of the universe. I figured "Hey, tons of new TLDs are coming out. I can have a short domain all to myself. This is great!". Fast forward to the future. I can't use it to post on Craigslist (which they won't even TELL you unless you get a customer service rep who knows that that is their unwritten "policy". You only get a meaningless error number and a note to check your post for errors). Tons of websites use retarded email validation code that only checks for specific domains or assumes that all TLDs have 3 characters. Latest example: my kid's orthodontist's website. It is also silently blocked by Child Protective Services so I can't email our adoptive son's worker.

      In short, it is a huge mess but I use this domain for everything and it will be a major pain to switch (like switching banks).

      -l

      --
      Help cure AIDS, cancer, and more. Donate your unused computer time to worldcommunitygrid.org. Join Team Slashdot!
    25. Re:Regexp-based address validation by Luyseyal · · Score: 1

      Bah, it's still a bad idea. ICANN adds more TLDs every year and soon Microsoft and Google will have .microsoft and .google and there will be Unicode domain names soon enough. It's a terrible, terrible idea for websites to bother validating them outside of actually using them. Require double-entry and send a "click here to complete registration" URL in an email. That's it. Anything else is a recipe for failure on the Web.

      Eh, I should step back and say, "it's a bad idea, on the Web". I can see legitimate use of such code in, say, data mining.

      -l

      --
      Help cure AIDS, cancer, and more. Donate your unused computer time to worldcommunitygrid.org. Join Team Slashdot!
    26. Re:Regexp-based address validation by Luyseyal · · Score: 1

      ... probably due to the large amounts of contiguous spaces. Don't really know.

      -l

      --
      Help cure AIDS, cancer, and more. Donate your unused computer time to worldcommunitygrid.org. Join Team Slashdot!
    27. Re:Regexp-based address validation by mutende · · Score: 1

      Beautiful? That thing is hideous.

      It seems most people in this thread forgot to turn on their humor meter...

      --
      Unselfish actions pay back better
    28. Re:Regexp-based address validation by Lost+Race · · Score: 1

      It already fails on .museum and .travel domains.

    29. Re:Regexp-based address validation by Luyseyal · · Score: 1

      I must say, I'm disappointed by the lack of "itbelongsina.museum".

      -l

      --
      Help cure AIDS, cancer, and more. Donate your unused computer time to worldcommunitygrid.org. Join Team Slashdot!
    30. Re:Regexp-based address validation by Tet · · Score: 1

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

      Grrr. I wish people wouldn't wheel this out every time someone mentions regular expressions. Simply because it's plain wrong, and written by someone who either doesn't understand RFC 822 or doesn't understand regular expressions (I'm not entirely sure which)

      --
      "The invisible and the non-existent look very much alike." -- Delos B. McKown
    31. Re:Regexp-based address validation by kaens · · Score: 1

      Well, the module will let you do that. The regexp will only work on addresses that have comments stripped out.

      Of course, I don't think that I've ever seen a comment in an email address (I'm too young for that).

    32. Re:Regexp-based address validation by mutende · · Score: 1

      I can insert comments into a mail address????? How nice! How do I do that?

      I guess it's the parantheses they're talking about. In an email address like

      john.doe@example.com (John Doe)

      the name John Doe is a comment.

      --
      Unselfish actions pay back better
    33. Re:Regexp-based address validation by Atario · · Score: 1

      The one I always use does the same thing:

      /.+@.+\..+/

      And it has the advantage that it fits on a regular CD-R.

      --
      "A great democracy must be progressive or it will soon cease to be a great democracy." --Theodore Roosevelt
    34. Re:Regexp-based address validation by Anonymous Coward · · Score: 0

      like this: Mohammed(I am the Greatest)Ali@foo.bar

    35. Re:Regexp-based address validation by amorsen · · Score: 1

      Double entry is useless, everyone uses copy-and-paste. Anyway, running the address through an RFC validator is a perfectly fine thing to do. A validator less liberal than the RFC is a no-go, of course.

      --
      Finally! A year of moderation! Ready for 2019?
    36. Re:Regexp-based address validation by Luyseyal · · Score: 1

      1) a) Everyone does not use copy and paste. b) That's beside the point anyway. The point of double-entry is so the user looks at their email address twice.

      2) If you need a valid email on a web form, send a trackback URL.

      3) There's no point in checking for RFC-compliant addresses because it's a waste of time and most coders do it poorly anyway. Let the mail server handle it (see [2]). If the address is eaoouaeoa@sthtnhnshnssth.com, you have an RFC-compliant address that goes nowhere. So you're screwed anyway without a trackback.

      Lastly, I'm only talking about Web forms here. If you're crawling the Web for addresses or doing some data mining or something, go ahead and validate to your heart's content. But it is foolhardy to try and validate an email address in a few lines of Javascript because inevitably you will do it wrong and the number of valid email addresses that go nowhere is unbounded.

      -l

      P.s., this is from personal experience as a legitimate user of a .info domain. I can't tell you how many websites out there are completely broken if you don't have a .com/.net/.org/.edu email address.

      --
      Help cure AIDS, cancer, and more. Donate your unused computer time to worldcommunitygrid.org. Join Team Slashdot!
    37. Re:Regexp-based address validation by amorsen · · Score: 1

      If the address is eaoouaeoa@sthtnhnshnssth.com, you have an RFC-compliant address that goes nowhere. So you're screwed anyway without a trackback.

      Not screwed at all. I know that as long as the rest of the code can handle RFC-compliant addresses, there won't be any security vulnerabilities.

      Validate as much as possible, as early as possible. That leads to the best user experience and the least risk.

      --
      Finally! A year of moderation! Ready for 2019?
    38. Re:Regexp-based address validation by Luyseyal · · Score: 1

      1) I expect you could make a SQL injection attack out of a carefully crafted RFC-compliant email address. Why? Because the charspace is huge and allows plenty of funny business. However, with bounds checking (length < whatever) and bind variables, what's the worry? Do you write all your web forms in C?

      2) You have to validate server-side anyway because you can't trust JavaScript. Furthermore, you don't know if it's a genuine email address anyway until you try it.

      3) The codemonkeys I'm talking about typically use JavaScript for "validation" (I put it in quotes, because most code it incorrectly and wouldn't know what an SQL injection was if it bit 'em in the server).

      Lastly, it hasn't given me the "best user experience" when other "programmers" have tried to validate my address. The most useful advice is "don't". I'm going to abuse a quote here about TCP/IP: Most email validators implement the spec, badly.

      If makers of JavaScript want to do the world a favor, they should implement a built-in for checking addresses for RFC822 compliance. Because for regular Joes two steps above coding-by-drag-n-drop, it really is Too Hard[tm]. This is what standard libraries are for, anyway, and email addresses are part and parcel to the Internet.

      Anything is possible with a Turing machine, including running it poorly.
      -l

      --
      Help cure AIDS, cancer, and more. Donate your unused computer time to worldcommunitygrid.org. Join Team Slashdot!
  9. Mainframe Formatting by jchawk · · Score: 1

    I use this to remove formatting that is included in the reports spit out from the mainframe -

    cat REPORT_NAME | sed 's/[^a-z0-9,.-]//gi' > REPORT.out

    It uses a few commands to accomplish this but I figured I would include the entire command line for completeness. It keeps all letters, numbers, ',', '.', and '-'. If you need other characters you can always add them to the regular expression.

    1. Re:Mainframe Formatting by msuarezalvarez · · Score: 3, Insightful

      You are a great candidate for the Useless Use of Cat award... specially endearing is your making a comment on the few commands your line uses :D

    2. Re:Mainframe Formatting by angrytuna · · Score: 2, Funny

      That was a new one on me; I hadn't encountered that award before. Would something like:

      <REPORT_NAME sed 's/[^a-z0-9,.-]//gi' > REPORT.out

      be preferable in this instance?

      --

      It is a solemn thought: dead, the noblest man's meat is inferior to pork.

    3. Re:Mainframe Formatting by neurovish · · Score: 1

      That was a new one on me; I hadn't encountered that award before. Would something like:

      <REPORT_NAME sed 's/[^a-z0-9,.-]//gi' > REPORT.out

      be preferable in this instance?

      Personally, I'd go with sed 's/[^a-z0-9,.-]//gi' REPORT_NAME > REPORT.out, but maybe that's just the version of sed I'm using?

    4. Re:Mainframe Formatting by Luyseyal · · Score: 1

      You could use "< Report_Name".

      -l

      --
      Help cure AIDS, cancer, and more. Donate your unused computer time to worldcommunitygrid.org. Join Team Slashdot!
    5. Re:Mainframe Formatting by dirtyhippie · · Score: 1

      How about good ol' strings(1)?

  10. 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

    1. Re:Windows by Anonymous Coward · · Score: 0

      I use eclipse as a C++ editor (don't kill me /.). It can use regex in the search/replace, which is really handy.

    2. Re:Windows by Anonymous Coward · · Score: 0

      In Word, you can select "use wildcards" in the find utility to enable regex-like features.

  11. is it an rfc-822 compliant e-mail address? by Anonymous Coward · · Score: 3, Interesting

    please validate using the rfc and not your sketchy interpretation of an e-mail address. /.*@.*\..*/ will not cut it.

    Try instead
    ([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x22)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d))*

    See the original at http://www.iamcal.com/publish/articles/php/parsing_email/

    1. Re:is it an rfc-822 compliant e-mail address? by Timmmm · · Score: 3, Insightful

      Mmmmm readable.

    2. Re:is it an rfc-822 compliant e-mail address? by Anonymous Coward · · Score: 0

      Please validate using the rfc and not your sketchy interpretation of an e-mail address. ([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x22)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d))* will not cut it.

      Try instead http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

      Honestly, unless yours is perfect, don't knock other people's as if you were god.

    3. Re:is it an rfc-822 compliant e-mail address? by Rikiji7 · · Score: 1

      Agree.

      --
      slashwhat?
    4. Re:is it an rfc-822 compliant e-mail address? by feldicus · · Score: 0, Troll

      It's things like that monstrosity that reinforce my belief that learning the ins and outs of regular expressions isn't worth the work.

      feldicus

    5. Re:is it an rfc-822 compliant e-mail address? by Ken+D · · Score: 3, Interesting

      The problem is that email addresses are not suitable for regex based validation.
      There are too many legacy formats, too many variations, that are legal addresses.

      Why, back in the old days, you could send mail to things like "bob%example.com@example.org" which would shoot the email off to example.org, who's mail server would then shoot the email off to example.com. A way to hand route your email around a broken network link in the old days. Throw in a few UUCP hops, maybe getting final delivery to a BITNET connected system. Ah, those were the days!

    6. Re:is it an rfc-822 compliant e-mail address? by caluml · · Score: 1

      If I have to validate an email address, I just look up the MX record for the bit after the @. If it exists, and the bit before the @ looks reasonable, it's good enough for me.
      If I have to validate an IP address in Java, I just create a new InetAddress. If it fails, it wasn't valid. That too, is good enough for me.

    7. Re:is it an rfc-822 compliant e-mail address? by Lumpy · · Score: 1

      WOAH! that hurts my eyes!

      --
      Do not look at laser with remaining good eye.
    8. Re:is it an rfc-822 compliant e-mail address? by tirerim · · Score: 1

      Most of that is just using hex codes for perfectly good ASCII characters -- if you used the characters themselves, some of them (most of the non-alphanumeric ones) would have to be escaped, but it would be a lot more readable, and would work the same way.

    9. Re:is it an rfc-822 compliant e-mail address? by ais523 · · Score: 2, Insightful

      Does that thing allow nested comments, and escaping inside them? It doesn't look like it, it isn't recursive. (I have some in the email address I typically put online, ais523(524\)(525)x)@bham.ac.uk; that could be a good test for your email client, and is useful because I've never come across a spambot that can parse it.)

      Recent versions of Perl and Python regices allow you to write recursively; that probably qualifies as a stupid regex trick, especially as it makes them more computationally powerful so they can handle things like email addresses. Or you could just sit wondering why email addresses allow nested comments anyway...

      --
      (1)DOCOMEFROM!2~.2'~#1WHILE:1<-"'?.1$.2'~'"':1/.1$.2'~#0"$#65535'"$"'"'&.1$.2'~'#0$#65535'"$#0'~#32767$#1"
    10. Re:is it an rfc-822 compliant e-mail address? by trjonescp · · Score: 2, Informative

      The link provided by the parent provides a more readable version of the same thing (written as a PHP function)

      --
      Only speak when it improves the silence.
    11. Re:is it an rfc-822 compliant e-mail address? by Sanat · · Score: 1

      That is why it is called "Write Only" code.

      Keeping the mind trained on the issues as it is surmised will produce this kind of code, yet in a week or so the ability to read it (unless it is commented) approaches nil.

      I have written stuff like this and to my amazement (and horror) it works. Without comments though it makes the updating process very difficult... even with comments it makes the updating very difficult come to think of it.

      YMMV

      --
      And in the end, the love you take is equal to the love you make
    12. Re:is it an rfc-822 compliant e-mail address? by Anonymous Coward · · Score: 0

      Tsk. Way too long. Here's a shorter one that matches email addresses:

      .*

      Interestingly, it can also be used to match various other things too. :-)

    13. Re:is it an rfc-822 compliant e-mail address? by mebrahim · · Score: 1

      But are Regexes supposed to be human-readable?

    14. Re:is it an rfc-822 compliant e-mail address? by cleatsupkeep · · Score: 1

      What's the regex for "looks reasonable"?

    15. Re:is it an rfc-822 compliant e-mail address? by DoubleDownOnEleven · · Score: 1

      I did something similar with javascript a few years ago. The javascript is used to codify the RFC BNF, which then generates the regex.

      http://www.digitalxen.net/files/emailValidation.js

      The other one was a regex for validating phone numbers (at least in the US). It was based on standards from NANPA and ATIX. It worked great until a phone company "accidently" started issuing numbers using one of the exchange codes set aside for testing.

      http://www.digitalxen.net/files/phoneValidation.js

    16. Re:is it an rfc-822 compliant e-mail address? by TheoMurpse · · Score: 1

      Why, back in the old days, you could send mail to things like "bob%example.com@example.org" which would shoot the email off to example.org, who's mail server would then shoot the email off to example.com. A way to hand route your email around a broken network link in the old days. Throw in a few UUCP hops, maybe getting final delivery to a BITNET connected system. Ah, those were the days!

      It's tales like this that Slashdot preserves for posterity. I love it!

    17. Re:is it an rfc-822 compliant e-mail address? by caluml · · Score: 1

      a-Z, 0-9, -, and .

    18. Re:is it an rfc-822 compliant e-mail address? by badkarmadayaccount · · Score: 1

      I am not worthy. *bows*

      --
      I know tobacco is bad for you, so I smoke weed with crack.
    19. Re:is it an rfc-822 compliant e-mail address? by RichiH · · Score: 1

      Do you have many problems with MUAs & MTAs not being able to send to you? Would you even find out? This is a neat trick and I might adopt it :)

    20. Re:is it an rfc-822 compliant e-mail address? by ais523 · · Score: 1

      Most websites don't accept it, in my experience (probably because their email validation regexps don't handle nested comments). Most low-level mailing stuff will strip the comments at some stage before it gets out of the computer sending it, meaning everything works fine; old-fashioned command-line mailing programs work fine with them, usually. More modern GUI stuff is a bit less good at handling it; Outlook gets completely confused, Thunderbird handles comments but not nested comments, and Evolution escapes the backslash in the comment, assuming I meant a literal backslash in the comment not to escape the paren, so the message bounces.

      --
      (1)DOCOMEFROM!2~.2'~#1WHILE:1<-"'?.1$.2'~'"':1/.1$.2'~#0"$#65535'"$"'"'&.1$.2'~'#0$#65535'"$#0'~#32767$#1"
    21. Re:is it an rfc-822 compliant e-mail address? by RichiH · · Score: 1

      Hmm, I think I will stick to my (pretty awesome) scheme of simply making sure my main address appears _nowhere_ on the intarwebs. At some point, I will need to create an account that accepts gpg-signed email, only.

      Thanks for the info! :)

  12. 99 Bottles of Beer on the wall by Pahalial · · Score: 2, Interesting

    Saw this one recently, by Andrew Savige. He did use a Perl module to generate the regex itself, but even so!

    http://search.cpan.org/dist/Acme-EyeDrops/lib/Acme/EyeDrops.pm#99_Bottles_of_Beer

    (I would quote the final result but /. won't allow that many "junk" characters.. let's hope that doesn't cripple this entire discussion.)

    --
    Stuff.
    1. Re:99 Bottles of Beer on the wall by Culture20 · · Score: 4, Insightful

      (I would quote the final result but /. won't allow that many "junk" characters.. let's hope that doesn't cripple this entire discussion.)

      Interesting that a site for nerds doesn't allow a lot of characters commonly used in source code.

    2. Re:99 Bottles of Beer on the wall by jonadab · · Score: 1

      Slashdot is a *news* site for nerds, not a programming site. For the latter, head over to Perlmonks or someplace like that, and you can post regular expressions until your eyes glaze over.

      --
      Cut that out, or I will ship you to Norilsk in a box.
    3. Re:99 Bottles of Beer on the wall by Anonymous Coward · · Score: 0

      There has to be somewhere you draw the line. Otherwise you'd get everyone posting in brainfuck.

  13. Regex Bill by Anonymous Coward · · Score: 5, Funny

    Why couldn't Bill try out his regular expressions?

    His mom wouldn't let him play with matches.

  14. negative lookahead assertion by Anonymous Coward · · Score: 0

    Example of using negative lookahead assertion to parse comma delimited data:

    perl -pe 's/(^|,)\\\\N(?=,|\$)/\$1\$2/g'

  15. PCRE and perl 5.10 offer "tagged" captures by BrianRoach · · Score: 1


    (?:<thing>foo)

    Where you can then access the matched substring ("foo" in this case) by the tag/label "thing" (access syntax depends on language).

    It's pretty spiffy if you need order independent matching.

    1. Re:PCRE and perl 5.10 offer "tagged" captures by gzipped_tar · · Score: 2, Informative

      That's also one of my favorite. Python has this feature too.

      --
      Colorless green Cthulhu waits dreaming furiously.
    2. Re:PCRE and perl 5.10 offer "tagged" captures by TheoMurpse · · Score: 1

      Yes, although the syntax is different
      a = foo.search("(?P<label>regexstring)",wall_of_text) will let you then look at a.group('label') if I recall correctly.

  16. extract web address from a string? by bundaegi · · Score: 1

    This what I ended-up using:

    ((?:http|ftp)s?://)?(((([\d]+\.)+){3}[\d]+(/[\w./]+)?)|([a-z]\w*((\.\w+)+){2,})([/][\w.~]*)*)

    There may well be something more robust...

    --
    bundaegi is good for you
    1. Re:extract web address from a string? by PRMan · · Score: 1

      I use this in .NET:

      ^(?<protocol>https?\:\/\/)?(?<domain>[\w-]+\.)+(?<tld>[0-9]{1,3}|aero|arpa|biz|com|coop|edu|gov|info|int|museum|net|org|[a-z]{2})(?<port>\:[0-9]{1,5})?\/?(?<filepath>[~\w,-\.\(\)%]+\/?)*\??(?<qs>\w+\=[+\w\$\-\%\{\}\(\)]*\&?)*(?<anchor>\#\w+)?

      It's not perfect, but it matches everything I've ever thrown at it. It's also nice because it gives you named groups with all the parts in it.

      --
      Peter predicted that you would "deliberately forget" creation 2000 years ago...
    2. Re:extract web address from a string? by Anonymous Coward · · Score: 0

      When did FTP become the "web"?

    3. Re:extract web address from a string? by bundaegi · · Score: 1

      When did FTP become the "web"?

      About the same time gopher was being phased out?

      Yes, if you want to be pedantic, I meant URL, not web address (since you so cleverly pointed at the ftp/ftps schemes). Here's my geek card, I give up...

      --
      bundaegi is good for you
  17. Silence is golden by MikeRT · · Score: 1

    For that annoying user who never shuts up... /[.]//

    What's that? Cat got your tongue, troll?

    1. Re:Silence is golden by Goaway · · Score: 1

      Nope, still here.

    2. Re:Silence is golden by Anonymous Coward · · Score: 0

      what do you have against periods?
      Don't you mean something more like: /.//g

    3. Re:Silence is golden by Goaway · · Score: 1

      PS: I totally messed that up Should've been "Nope, still here".

    4. Re:Silence is golden by omnipresentbob · · Score: 1

      He probably did
       
      Or maybe he was thinking woman are enough of a pain without their periods (there would be an ellipsis here, but they were all replaced)

    5. Re:Silence is golden by SCHecklerX · · Score: 1

      surely you meant /.*//g /[.]// just removes the first period it sees.

  18. Match a library call number by Gulthek · · Score: 4, Interesting

    Here's a chunk of perl script I wrote (years ago) that determines if $text matches any of the styles of library call number that I've ever encountered.

    Slashcode is interestingly interpreting my formatting, but you should get the gist.


    $text =~ /
            ^[A-Z]+ # starts with at least one capital letter
            \s? # followed by an optional space
            \d+ # followed by one or more digits /x
        or $text =~ /
            ^\d+ # starts with one or more digits
            \. # followed by a single decimal /x
        or $text =~ /
            \d+ # starts with one or more digits
            \s # and a space /x
        or $text =~ /
            Thesis # starts with "Thesis" .+ # with one or more characters of any kind
            \d{4} # then four numbers - year
            \s+ # separated by at least one space
            [A-Z]+ # from one or more capital letters
            \d+ # followed by one or more numbers /xi # case ignored here in case we run into THESIS or thesis
        or $text =~ /
            \d+ # starts with one or more digits
            \- # connected with a dash
            \d+ # to one or more following digits /x
        or $text =~ /
            \d+ # starts with one or more digits
              # followed by a space
            [A-Z]* #followed by zero or more capital letters
        \d+ # followed by one or more digits /x

    1. Re:Match a library call number by mgbastard · · Score: 4, Funny

      holy crap. You DOCUMENTED your regular expression? You shall be thrown into the pit!

      --
      Anyone seen my low uid? last seen 10 years ago while panning the #@$# out of Taco's 'web based discussion system'
    2. Re:Match a library call number by Gulthek · · Score: 1

      Yeah, after the fourth or fifth time I found *yet another* strange exception to my increasingly complex rule/logic I figured that it was worth it.

      Honestly when I first decided to document it I didn't even know it was possible to do it inline.

      And, huh, looking at it again the code is missing a bunch of ^'s (starts with).

    3. Re:Match a library call number by quist · · Score: 1

      YEA! I'll have company!
      Welcome to the pit, Gulthek... you'll find, down here, we may not remember what we did yesterday, but we can understand it if needed.

    4. Re:Match a library call number by Anonymous Coward · · Score: 0

      \d+ # starts with one or more digits
      # followed by a space
      [A-Z]* #followed by zero or more capital letters

      What space? You're using /x mode, you need to specify \s for spaces,

  19. how about a regex by nimbius · · Score: 1

    to filter ask slashdot posts for 'tricks' articles.

    --
    Good people go to bed earlier.
  20. Nope, not useful by darkvizier · · Score: 4, Funny

    I've never found regexes to be useful at all. I prefer to write my own parsers from scratch in assembly language, or conway's game of life, if I'm feeling m/(ambitious|artistic|autistic|masochistic)/.

    But even an artist gets lazy sometimes.

    1. Re:Nope, not useful by tirerim · · Score: 1

      Yeah, if you were feeling that, you would have written it m/(ambitious|(art|aut|masoch)istic)/. Though I'm not sure why you needed to capture the whole thing, either -- not sure how you're feeling?

    2. Re:Nope, not useful by Anpheus · · Score: 1

      I wrote a regex parser for a turing machine implemented in Conway's Game of Life.

      Sorry.

    3. Re:Nope, not useful by Anpheus · · Score: 1

      (I'm almost done compiling Linux 0.02, by the way.)

  21. One for weeding out rubbish by Krigl · · Score: 1

    perl -pe '$IFS="EOF",s/^.*$//' file_with_lots_of_rubbish Works like a charm! Especially useful for dealing with marketing papers, Czech journalism and Slashdot discussions.

    --
    Troll 2.0 Fear my asocial networking!
  22. in perl by mqhiller · · Score: 1

    I like:

    scalar s/\|/\|/g;

    returns number of vertical bar ('|') characters in the variable $_.

    mqh

  23. Coincidence by Anonymous Coward · · Score: 0

    I just discovered a good regexp used to check file permissions : http://thedailywtf.com/Articles/Now-I-Have-Two-Hundred-Problems.aspx

  24. Apocryphal quotation by Stavr0 · · Score: 1

    Some people, when confronted with a problem, think âoeI know, I'll use regular expressions.â Now they have two problems. -- Jamie Zawinski

  25. CWEB and Doxygen by N3Roaster · · Score: 1

    Here's one I came up with recently:

    If you want to get documentation out of both CWEB and Doxygen, write the Doxygen comments in the source files like @=//! Comment for Doxygen.@> to prevent ctangle from stripping the comment out, then use sed 's/@=\/.*@>//g' input.w > output.w to strip those comments out so they don't end up in the output from cweave.

    --
    Remember RFC 873!
  26. 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.
    1. Re:One regex to match them all by dkf · · Score: 1

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

      You've omitted the ability to match other number bases. I don't know about you, but I find it useful to be able to cope with numbers in base 2, base 8 and base 16 as well (both as integers or floats, of course).

      --
      "Little does he know, but there is no 'I' in 'Idiot'!"
    2. Re:One regex to match them all by nadavwr · · Score: 1

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

      But does it run Linux?

    3. Re:One regex to match them all by Anonymous Coward · · Score: 0

      /.*/

    4. Re:One regex to match them all by Anonymous Coward · · Score: 0

      Note that it doesn't match all floating-point values. Specifically, it doesn't match "inf" or "nan", which are valid insofar as they can be generated by printf("%f") and read back in by scanf("%f"), atof(), strtod() etc.

    5. Re:One regex to match them all by Atario · · Score: 1

      You forgot comma separators, currency symbols, and parentheses for negative.

      Hey, now I see why users are always pulling this crap on me. It's fun!

      --
      "A great democracy must be progressive or it will soon cease to be a great democracy." --Theodore Roosevelt
  27. use Regex::Common; by oneiros27 · · Score: 4, Insightful

    use Regex::Common qw(URI net);
    $text_with_urls =~ m/$RE{URI}/;
    $text_with_ips =~ m/$RE{net}{IPv4}/;

    --
    Build it, and they will come^Hplain.
  28. news for nerds. NERDS by circletimessquare · · Score: 2, Informative

    stuff that matters

    understand the concept?

    if not, try going to this site, it looks like it might be more your speed

    buhbye

    --
    intellectual property law is philosophically incoherent. it is your moral duty to ignore it or sabotage it
    1. Re:news for nerds. NERDS by Bill,+Shooter+of+Bul · · Score: 1

      Ok, but where does it stop?

      (Useful) Stupid Linux commands? type "ls " to list the contents of the current directory!

      (Useful) Stupid Window commands? The arrow on the screen moves in reaction to the movement of the small hand shaped device!

      My main complaint with these stores is the only ifnormation being posted is really basic. Its not (Useful) Stupid as advertised.

      --
      Well.. maybe. Or Maybe not. But Definitely not sort of.
    2. Re:news for nerds. NERDS by cbiltcliffe · · Score: 1

      The problem is, it sounds more like:

      News for wannabees. Stuff that everybody qualified already knows.

      As for a gossip site, what does that have to do with anything at all? Literally.

      --
      "City hall" in German is "Rathaus" Kinda explains a few things......
    3. Re:news for nerds. NERDS by scotsghost · · Score: 1

      (Useful) Stupid Window commands? The arrow on the screen moves in reaction to the movement of the small hand shaped device!

      hand-shaped? unless you're missing all your fingers, it's much more rock-shaped.

    4. Re:news for nerds. NERDS by Bill,+Shooter+of+Bul · · Score: 1

      Well, its shaped for the hand, not like it. a subtle distinction.

      --
      Well.. maybe. Or Maybe not. But Definitely not sort of.
    5. Re:news for nerds. NERDS by Anonymous Coward · · Score: 0

      basic is a relative term, this totally new shit for me, and I'm about as geeky as they come. Oh, did I mention I'm a physics(nuclear and weapons)/pharmacology(drugs) nerd, with a sprinkling of chemistry(explosives, poisons and drug synthesis)? I love UNIX and computers, but please bear in mind we are not all Real Programmers (TM).

  29. Remove trailing whitespace by cerberusss · · Score: 3, Interesting
    To remove trailing whitespace from a textfile (vim regex, don't know if the \s will work in other regex dialects):

    /\s\+$//e

    --
    8 of 13 people found this answer helpful. Did you?
    1. Re:Remove trailing whitespace by Niek+Kouwenberg · · Score: 1

      Or removing whitespace from each line of code (replace with \\1):
      [\t ]+(\r?\n|$)

      Removes only tabs and spaces, since newlines should be preserved. It just doesn't trim trailing newlines at the end of the file.

    2. Re:Remove trailing whitespace by orkysoft · · Score: 1

      I think you mean /\s+$//e

      --

      I suffer from attention surplus disorder.
    3. Re:Remove trailing whitespace by legirons · · Score: 1

      why you escape the +?

    4. Re:Remove trailing whitespace by Anonymous Coward · · Score: 0

      No, because a + finds a plus sign. You need to use \+ to mean "one or more". You're used to extended regular expressions, and (at least by default) vim uses basic regular expressions.

  30. replace by Anonymous Coward · · Score: 0

    s/bush/obama/ig

  31. 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

    1. Re:Do these questions really belong here? by Moebius+Loop · · Score: 2, Insightful

      I like stackoverflow a lot and have been tangentially involved in other tech knowledge base-type sites, but they suffer from one typical problem.

      People who already *have* certain knowledge don't often spend much time reading sites dedicated to dispensing that information.

      --
      have you been seen on slash?
  32. Not very complex, but ... by Bob-taro · · Score: 1

    I often use sed to split a delimited line into multiple lines. E.g.:

    echo $PATH | sed 's/:/\
    /g'

    --
    Prov 9:8 Do not rebuke mockers or they will hate you; rebuke the wise and they will love you.
    1. Re:Not very complex, but ... by msuarezalvarez · · Score: 1
      Use tr to change single characters:

      echo $PATH | tr : \\n

  33. Just read this by Anonymous Coward · · Score: 2, Informative
  34. RFC 822 email validation by gpuk · · 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/

    1. Re:RFC 822 email validation by gpuk · · Score: 1

      Damn - that hurts my brain!

    2. Re:RFC 822 email validation by BauerUK · · Score: 1

      Really? That makes me wet.

    3. Re:RFC 822 email validation by gpuk · · Score: 1

      rofl!

  35. Be lazy! by subreality · · Score: 4, Interesting

    OK, you asked for stupid tricks, but this one's just plain lazy.

    Between bash and grep, there are quite a lot of special characters that you have to escape... Or just ignore with dots!

    /I.do.this.frequently..(even.with.parenthases).,.because.sometimes.my....backslash..key.is.tired/

    A couple neat things happened: The extra dot after frequently is matching an inline paren. The paren in the PATTERN right next to it starts the mark of an atom, closed by its brother. The comma is because I put one outside the paren (here represented as the dot to the left of the comma) as is my style. Also note the literal backslash, just before you see the word backslash in hidden parenthesis.

    Why not add quotes to match the spaces easily? I get a word or two in, and I find I naturally switch to using dots. These are throwaways for single tries through grep. For production code, I hone in carefully on the parts that I'm dead sure I can anchor to, escaped by any means needed, before carefully choosing my atom to match as tightly as possible, so it'll error out if my data has gone wrong.

    Even in a simple case like this, half the fun is in explaining it. :)

    1. Re:Be lazy! by layingMantis · · Score: 1

      dude that is the ugliest regex I've ever seen, and there's some errors in your explanation:

      the parentheses are not escaped with a backslash (so they aren't literal parentheses) - all these do in a regex is "remember what's inside the parentheses in case I want to use it after running this match" (capturing group) - that is NOT "atomic grouping", which means that once an atomic group is matched it can never be "given back" or backtracked into by the engine trying to make the rest of the expression match.

      the DOT-COMMA-DOT, even if your ()'s were right, almost certainly makes no sense either. Your real comma would end up being matched by a dot somewhere and your expressions blows up.

    2. Re:Be lazy! by subreality · · Score: 1

      Let me demonstrate:

      /I.do.this.frequently..(even.with.parenthases).,.because.sometimes.my....backslash..key.is.tired/
      /I do this frequently ({even.with.parenthases}), because sometimes my \ (backslash) key is tired/

      (The atom is now marked with {}, the () are now literal)

  36. very topical! by Anonymous Coward · · Score: 0

    I've been trying to scrape a web page lately and have been trying to get a working regexp going without a huge amount of success....Anyone care to demonstrate their awesome skills by showing me how to write a regexp that will match words in data like this (or at least the first word in a string):

    " 1. Word AnotherAgain "

    The key point about the data is that there are words which start with exactly 1 capital letter, and may be seperated from the subsequent word by a number of spaces space or may run directly onto it. So the desired regexp would match Word or Another or Again depending which was the first in the data. There are also numbers e.g. "10." but it doesnt matter for my purposes whether they are discarded or matched as a word.

    1. Re:very topical! by bpjk · · Score: 2, Informative
      You didn't say if digits can be part of word that starts with a capital; I'm assuming they cannot be below, changing to accommodate that would be easy.

      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.

    2. Re:very topical! by Skrynesaver · · Score: 1

      So you want to catch something begining with an upper case letter and match until the next uppercase letter, number or space?

      print $1 if ($text =~ m/([A-Z][^A-Z 0-9]+)/);

      --
      "Linux is for noobs"-The new MS fud strategy
    3. Re:very topical! by Anonymous Coward · · Score: 0

      thank you

    4. Re:very topical! by Anonymous Coward · · Score: 0

      I implemented something based on this last night and it worked perfectly.

      Up til now I had been trying to use ^ to match from the beginning of a line, then match .* then match my word definition but for whatever reason it wasn't working.

      anyway you have tightened up my understanding of how exactly a pattern is matched (first, biggest occurrence of the pattern in a line)

      again, thank you

  37. 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
    1. Re:recursive regexp to match {} block by Anonymous Coward · · Score: 0

      Try this example: { return "Ooops} I guess you didn't think about string literals."; }.

      It's okay to use regexes for the tokenizer, but you're bound to screw up if you try to use a regex engine for the parser too.

      p.s. It's bad form to use Portuguese comments on an English forum. :P

  38. email validation... by Ramley · · Score: 2, Interesting

    This was always useful when appropriate: /^[\w.|-]+@(?:[\w.|-]{2,63}\.)+[a-z]{2,6}$/ Validates a valid email address (rfc 5322) -- although not taking into account an IP address (user@192.168.1.2)

    1. Re:email validation... by skeeto · · Score: 1

      Before you go off and try to write your own e-mail regex, consider that, in order to get it right, it is going to have to look like this.

      That's right. The regex weighs in at about 6KB. Don't write your own.

  39. best programming editor ? (for windows) by layingMantis · · Score: 1

    You're absolutely right about the crap regex support in most text editors. In my personal opinion, Dreamweaver CS3 is the best code editor I've used for regex and searching. It has standard regular expressions unlike dumbass contenders like Visual Studio or Ultraedit. It's missing some stuff, though, like named groups with named substitution, multiple line search, and (this is the worst part), the $ and ^ anchors don't seem to work. But none of that matters if your search is fucking slow (see: visual studio 2008): Dreamweaver CS3 search is very, very FAST - notepad++ simply chokes on directory searches, and all of the other lite little editors I've tried do too.

    Valid Phone Number Validation that allows extensions and virtually all the common ways to list a (US) number:
    /^[01]?[-\s\.]?\(?[2-9][0-9]{2}\)?([-\s\.]|(\s-\s))?[0-9]{3}([-\s\.]|(\s-\s))?[0-9]{4}\s?(([xX]\.?|(ext|EXT)\.?|\s)?\s?(?<![0-9])[0-9]{1,4})?$/

    valid:

    333 444 5555
    1-333-444-5555
    333.444-555
    333444555 4444
    333-444-555 ext. 123
    1-(340)333 5678
    333 444 555 x3456
    ...and lots more ways to fuck up a number (validly)

  40. some that I've used ... by ianare · · Score: 4, Interesting

    SSN
    ^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}$

    US phone with or without parentheses
    ^\([0-9]{3}\)\s?[0-9]{3}(-|\s)?[0-9]{4}$|^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$

    ISO Date (19th to 21st century only)
    ^((18|19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])$

    1. Re:some that I've used ... by LCValentine · · Score: 1

      The area code for US phone number should neither start with a 0 or a 1.

      0 = operator
      1-0 = operator / international
      1-1 = international

      For the same reason, the local prefix should also not start with a 0 (operator).

      This might be a little more useful.

      ^\([2-9]\d{2}\)\s?[1-9]\d{2}(-|\s)?\d{4}$|^[2-9]\d{2}-?[1-9]\d{2}-?[0-9]{4}$

    2. Re:some that I've used ... by Anonymous Coward · · Score: 0

      What's the difference between \d and [0-9]?

    3. Re:some that I've used ... by jonaskoelker · · Score: 2, Funny

      ISO Date (19th to 21st century only)
      ^((18|19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])$

      This regexp is ISO certified. The certificate is valid until 2009-02-31.

    4. Re:some that I've used ... by The_reformant · · Score: 1

      you guys regex's are lame. This one will match WAY more than those ones:

      /.*/

      --
      I have discovered a truly remarkable sig which this post is too small to contain.
    5. Re:some that I've used ... by jonaskoelker · · Score: 1

      ^((18|19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])$

      How about ([012][0-9]|3[01])? That'd make it more readable. And why limit yourself to 18xx to 20xx just because there are some dates that don't exist as western society switched to the Gregorian calendar?

      Btw, we didn't all do it at once. Oh yeah, great fun. Talk to Danes about 20th of February 1700, or to Frenchmen about 10th of October, 1582. You can watch the mayhem at http://en.wikipedia.org/wiki/Gregorian_calendar.

      Time is fun :)

    6. Re:some that I've used ... by ianare · · Score: 1

      Thanks for the corrections. I knew someone would find something to improve on ;-)

    7. Re:some that I've used ... by ianare · · Score: 1

      I limited to 18-20 because in the application I'm using it in there can be no dates older than that (US real estate). For sure you could change that if needed.
      Time is almost as fun as character encodings ;-)

    8. Re:some that I've used ... by encoderer · · Score: 1

      And in this spirit..

      US Zip Code, either 5-digits or 9 (eg 12345-6789)
      ^\d{5}$|^\d{5}-\d{4}$

      And the Canadian postal code:
      ^[a-zA-Z][0-9][a-zA-Z]\s?[0-9][a-zA-Z][0-9]$

    9. Re:some that I've used ... by Anonymous Coward · · Score: 0

      This does us phone numbers with or without dashes spaces or parentheses.

      \(?\b[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}\b

    10. Re:some that I've used ... by mccrew · · Score: 1
      Doing a regex to match a phone number is a question that I have often asked interview candidates who claim to be perl experts.

      Mostly I get blank stares, but I would consider the phone regex a good first cut. It gets the general format right, but matches certain nonsensical patterns (e.g. 000-000-0000).

      --
      Hey, Windows users, there is no such thing as "forward" slash, there is only slash and backslash.
    11. Re:some that I've used ... by ztransform · · Score: 1

      US phone with or without parentheses

      Ah the royal pain that is web-site validation of phone numbers.

      I'm having that problem now updating my Australian phone number into a UK banking website.

      There are times when one has to carefully think about whether regexp validation is as useful as it may initially appear.

      Perhaps a good strategy is using regexps to warn a user when they are trying something that appears stupid, but allow them to continue if they are determined to throw something through that the programmer didn't foresee..

  41. Not a trick, but a question. by Janek+Kozicki · · Score: 2, Interesting

    I was wondering with my friend someday if it's possible with regex to select a pattern which occurs twice or more times repeatedly in single line but is separated by undefined characters. For example I want to select only lines in which the same pattern "[FB][ot]o" occurs exactly two times (in example below . is any character, for clarity):
     
    ...Foo... - is not selected
    ...Foo...Bto... - is not selected
    ...Bto...Bto... - is selected

    a normal /[FB][ot]o.*[FB][ot]o/ would select the second and third case. But I only want the third case. The first occurrence would define my pattern, and second occurrence must exactly match it. Magic stuff like this is not working: /\([FB][ot]o\).*\1/ although that seems to be the closest description of what we wanted.

    --
    #
    #\ @ ? Colonize Mars
    #
    1. 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 *.

    2. 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?
    3. Re:Not a trick, but a question. by Anonymous Coward · · Score: 0

      That's because what you're looking for is the union of two languages:

      1) Double occurrence of the substring "Foo" (i.e. /Foo.*Foo/)
      2) Double occurrence of the substring "Bto" (i.e. /Bto.*Bto/)

      With grouping and |, the language of the union is:

      /(Foo.*Foo)|(Bto.*Bto)/ # warning: perl regex

      It's longer than your version, but it accurately recognizes the language you described. Can anyone else come up with a cleaner version?

    4. Re:Not a trick, but a question. by Dracorat · · 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

    5. Re:Not a trick, but a question. by phantomfive · · Score: 1

      With regular expressions, as long as you have a finite number of things that you are looking for, you can just list them out. In your case, it could be something like this:

      /(Bto.*Bto.*)|(Foo.*Foo.*)|(Boo.*Boo.*)|.......etc.

      Another trick, sometimes it is easier to make a state machine that matches what you are looking for and then convert it into a regular expression. The only kind of thing regular expressions can't match are things with infinite patterns, like one or more sets of balanced parentheses. If you say, balanced parentheses sets of less than a thousand parentheses.

      --
      Qxe4
    6. Re:Not a trick, but a question. by funkatron · · Score: 1

      Well that looks like a regular language so it should be possible, no idea how to do it tho.

      --
      "Welcome to our world. We are the wasted youth. And we are the future too." Yes, I know these are stupid lyrics.
    7. Re:Not a trick, but a question. by PsychosisC · · Score: 1

      Feh. If it doesn't have more backslashes than you can count on one hand, it's probably not worth writing.

      [1] ~/code/foo $ grep \\\([BF][to]o\\\).*\\1 foo.txt

      Seriously, the '/' at the end of the expression seems to be the culprit. If Solaris grep can handle this kind of back-reference, I am pretty sure anything that does regexes can handle this kind of backreference.

    8. Re:Not a trick, but a question. by Anonymous Coward · · Score: 0

      I'm 100% sure there is a better way, there may even be a one-liner available using purely regexp, but here's a long winded, I'm sure terrible, workaround in Perl.


      my @lines = ("...Foo...","..Foo..Bto...","..Bto...Bto...");

      foreach my $line (@lines)
      {
                  my $temp_line = $line;
                  while($temp_line =~ /([FB][ot]o)/)
                  {
                                my $match = $1;
                                my $counter = 0;
                                while($temp_line =~ s/$match//)
                                {
                                                $counter++;
                                }
                                if($counter > 1)
                                {
                                                print "Found $match $counter times in $line\n";
                                }
                  }
      }

    9. Re:Not a trick, but a question. by Anonymous Coward · · Score: 0

      People should not forget that not everything is regex-able. Regular grammars are the simplest one from automata theory; most geeks know about turing machines, but the other two (context-free and context-sensitive) are not very well known.

    10. Re:Not a trick, but a question. by currivan · · Score: 1

      At the risk of doing your homework for you, the answer to your question is that according to the Myhill-Nerode theorem the language you describe is regular if and only if the inner pattern ("[FB][ot]o") matches only a finite number of strings. So yes in your example, but /(a+).*?\1/ is not strictly speaking a regular expression even though it's allowed in perl.

  42. Handy links by Kozz · · Score: 2, Informative

    While I'm not providing any specific trick per say, on topic are a few useful links:
    http://www.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 - plenty of regex info there which is Perl specific, but of course extends to many other similar implementations

    --
    I only post comments when someone on the internet is wrong.
    1. Re:Handy links by Pope · · Score: 2, 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 for OS X, a handy little tool for testing regexes, so all was not lost.

      --
      It doesn't mean much now, it's built for the future.
  43. Ignore whitespace by Wildclaw · · Score: 1

    The number one trick for regular expressions is not a regular expression at all. It is simply the habit of always using the ignore whitespace flag to format and comment your regular expressions. Code maintenance and general readability is simply a must for any real developer.

    One liners are for show, not for actual usage.

  44. kekekekeke by soupforare · · Score: 1

    Construct additional pylons!

    --
    --- Do you believe in the day?
  45. Blasphemy by Shohat · · Score: 2, Informative

    There are no Stupid Starcraft Tricks.

  46. Next is... by fxkr · · Score: 1

    (Useful) Stupid Slashdot Tricks?

  47. Comment removed by account_deleted · · Score: 1

    Comment removed based on user account deletion

  48. who needs regex? by circletimessquare · · Score: 1

    whatever you have to validate, encode it as a form submission and bounce it against http://ask.slashdot.org/comments.pl, screen scrape the results, and if slashdot's lameness filter doesn't balk, consider it validated

    --
    intellectual property law is philosophically incoherent. it is your moral duty to ignore it or sabotage it
  49. Simple E-mail Address by LCValentine · · Score: 1

    Sure, you could try and capture every single possible character, but can you update those when you find a bug?

    KISS:

    /(
    /(
    [\w\/\!\#\$\%\^\&\*\+\-\=\?\_\`\{\|\}\~]
    [\w\/\!\#\$\%\&\*\+\-\=\?\^\_\`\{\|\}
    \~\.]{0,62} [\w\/\!\#\$\%\^\&\*\+\-\=\?\_\`\{\|\}\~]
    @
    [A-Z0-9]
    [A-Z0-9\.\-]{0,246}
    [A-Z0-9]\.
    [A-Z]{2,6}
    )/ix

    In order:

    • A "name" maybe a-z, 0-9, plus some symbols
    • No leading . on the name
    • No trailing . on the name
    • @
    • A "domain" maybe a-z, 0-9, and .
    • ... but the . may not lead
    • ... and the . may not trail

    Happy hunting.

  50. Not-Prime Number Test by heeen · · Score: 0

    Is the length of a string of ones not prime? /^1?$|^(11+?)\1+$/

  51. Every regex by ZxCv · · Score: 1

    is beautiful.

    --

    Perl - $Just @when->$you ${thought} s/yn/tax/ &couldn\'t %get $worse;
  52. Converting files by caffiend666 · · Score: 1

    Here's a shell command to convert base64 encoded files into javascript strings using regular expressions in VI:

    for var in *image*.b64; do vi -c ':1d|$g/^=*$/d|1,$s/^/\t\t"/|1,$s/$/" +/|$s/=*" +/" +/|$s/" +$/";/|wq' $var; done

    --
    Here's to losing my Karma Bonus again....
  53. Validating credit card numbers by hansamurai · · Score: 2, Interesting

    Does anyone know if the Luhn Algorithm can be implemented in regex only?

    http://en.wikipedia.org/wiki/Luhn_algorithm

    (sorry if I double post this... I swear I posted it 10 minutes ago)

    1. Re:Validating credit card numbers by LCValentine · · Score: 1

      The algorithm requires addition, multiplication, and technically modulus. I highly doubt it. The function is easy enough to write... 2-3 lines.

    2. Re:Validating credit card numbers by xenocide2 · · Score: 1

      Here's a hint: if you can, your regex aren't regular.

      --
      I Browse at +4 Flamebait

      Open Source Sysadmin

    3. Re:Validating credit card numbers by marcosdumay · · Score: 1

      Aren't regular expressions turing complete?

    4. Re:Validating credit card numbers by joke_dst · · Score: 1

      No, regular expressions can not capture the complexity of that formula since it's an algorithm without memory, but with a finite set of numbers the can describe every possible match. You'll end up with a regex that incredibly long though, e.g. for 3 digits:

      ^0(12|23|31|47|51|69|74|83|92|01)|1(12|23|31|47|51|69|74|83|92|01)... (etc)

      (It will be 5 times longer in total and the last digits aren't the correct one, it's only for showing the principle)

      So it's possible but impractical.

    5. Re:Validating credit card numbers by pjt33 · · Score: 1

      In a strict interpretation of "regular expression", they are equivalent to Chomsky type 3 grammars - the most restricted of the four levels in the Chomsky hierarchy. Turing completeness corresponds to equivalence to type 0 grammars. Of course, the monstrosities which Perl calls regexes are far from regular, and for all I know may be Turing complete.

    6. Re:Validating credit card numbers by phantomfive · · Score: 1

      No, not at all.

      --
      Qxe4
    7. Re:Validating credit card numbers by phantomfive · · Score: 2, Insightful

      Yes, actually, (despite what the other posters have said), you can, but it will be very complicated since you will be implementing something like your own multiplier in regex.

      The simplest way to do it, of course, is to just list all valid Luhn Algorithm numbers. something like (.....384848583 | 938484845 | 8383838383......). Of course, this is probably not what you are looking for, because you will be listing a lot of numbers, and if your Luhn number is too big, then it won't be in your list.

      So, as for a more general solution, it is possible because at each digit you can know whether your number matches so far or not. What you will be basically implementing is a regular expression that checks each digit and says, "does this digit move me to a state that is a valid number or an invalid number?" I could be wrong, but my initial estimate is that this will take less than a thousand states in a state machine (of course, the easiest way to do this is to design a state machine and then translate it to a regular expression).

      To give an idea of what you are up against (and to help me find the answer to your question myself!) I implemented here a simple regular expression to determine if any binary addition will have an overflow at the last digit or not:

      ((0+1)+(1|(0+11)1+)+

      You can do something similar, although much much longer, with the Luhn algorithm.

      Hope that helps.

      --
      Qxe4
    8. Re:Validating credit card numbers by currivan · · Score: 1

      Yes, because it requires you to keep track of only a finite number of states to know everything you need to know about how you got to any given point in the sequence. That's the test for a regular language under the Myhill-Nerode theorem. However, writing the regex is going to be much more complicated than designing the state machine. I worked out the regex for whether an integer is divisible by three a month or so ago and it's already pretty complicated although it works on the the same principle with fewer states. The Luhn one will be exponentially worse.

  54. I tried to submit some by Anonymous Coward · · Score: 0

    i tried to submit some of my Regex Tricks
    but the web post complains about all the irregular characters

  55. that expression is a run-on sentence maker by layingMantis · · Score: 1
  56. TheDailyWTF by corbettw · · Score: 1

    Interesting that today's top story at TheDailyWTF is all about regexes, too. Except there, they're showing a case when you should NOT use it. I think a few of the people posting here need to take the quote in that article to heart:

    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. â" Jamie Zawinski

    http://thedailywtf.com/Articles/Now-I-Have-Two-Hundred-Problems.aspx

    --
    God invented whiskey so the Irish would not rule the world.
  57. Search through phone numbers by cerberusss · · Score: 4, Funny
    This regex goes through my enormous list of girlfriends' telephone numbers, and makes a selection based on the area code I'm currently in!

    #$%^&*(&^%{{}}{/\/\||```

    (No, that's not a regex at all. And no, I don't even have a single girlfriend.)

    --
    8 of 13 people found this answer helpful. Did you?
    1. Re:Search through phone numbers by Spittles · · Score: 1

      I'd find that more believable if that really was a regular expression and you didn't have a girlfriend.

    2. Re:Search through phone numbers by Anonymous Coward · · Score: 0

      Your girlfriend wouldn't exactly be single, would she?

  58. how many of you... by dlb · · Score: 0, Offtopic

    took the examples posted here and blindly ran them on your production system? how many did it as root?

  59. I can't believe noone has mentioned xkcd by laejoh · · Score: 0

    Just what I needed: the killer must have followed her on vacation but to find them we have to search through 200MB of emails looking for something formatted like an address!

  60. Useful parsing configs in bash by Bandman · · Score: 2, Interesting

    I have to parse files with bash sometimes, and I use these:

    ^# = line with a leading comment
    ^$ = empty line

    They're simple, but work usually. You can make them a lot more bullet proof by adding in blank checking between the characters, but it seems to work.

    cat httpd.conf | grep -v \^\# | grep -v \^\$ | less

    makes httpd.conf a lot more readable.

    1. Re:Useful parsing configs in bash by tapanitarvainen · · Score: 1

      I prefer

      grep '^[^#]' httpd.conf | less

      for the same purpose.

    2. Re:Useful parsing configs in bash by Bandman · · Score: 1

      Nice! Thanks!

    3. Re:Useful parsing configs in bash by Bandman · · Score: 1

      wait, did you mean

      grep '^[$#]'

      ?

    4. Re:Useful parsing configs in bash by Anonymous Coward · · Score: 0

      Your regex '^[$#]' matches lines that begin with a # or a $. '$' doesn't mean the end of a line in a character class, like you have it; it's literal there. A variant of that regex that should work is '^($|#)', so long as you use the -v inversion flag.

      '^[^#]' matches lines that don't start with '#'. More specifically, it matches a line that starts with a character that's not a '#'. This also ensures that the line is not empty: any character other than '#' will cause the line to match, so long as said character exists.

    5. Re:Useful parsing configs in bash by tapanitarvainen · · Score: 1

      No, that doesn't do the same.

      The pattern '^[^#]' matches lines that have a non-# character in the beginning, i.e., all but empty lines and lines beginning with #.

    6. Re:Useful parsing configs in bash by Michael+Wardle · · Score: 1

      sed -ie '/^#/d;/^$/d' httpd.conf makes httpd.conf much more readable!

      Seriously, the default format for that file is way too verbose.

    7. Re:Useful parsing configs in bash by dramaley · · Score: 1

      I prefer this slight modification because it strips comments that don't start in the first column:
      egrep -v '^\s*(#|$)' inputfile | less

      --
      ----- "I'm still sane on three planets and two moons."
  61. So something like... by tylerni7 · · Score: 1

    (Useful) Stupid               Tricks

  62. CAPTURES by ioudas · · Score: 0

    If RegXPChecker("^" & cmdPrefix & "rejoin (?<item>.+)", Message) Then
                    re = New Regex("^" & cmdPrefix & "rejoin (?<item>.+)")
                    mt = re.Match(Message)
                    item = mt.Groups("item").ToString
                    SendInfo("PART :" & item, SendBytes, tnSocket)
                    SendInfo("JOIN :" & item, SendBytes, tnSocket)
                    Return True
                End If

    Simple vb.net regex capture. same logic can be applied to other captures.

    --
    http://www.cushingproductions.com
  63. Rubular Regex Tester and DNS Zone Parser by Anonymous Coward · · Score: 0

    http://www.rubular.com/ is a great site for checking your regex.
     
    /^\s*(in|)(\S*)\s*(in|)\s*(mx|ns|a|cname)(\s*[0-9]{1,3}|)\s+(.+)$/ should parse most dns records out of a zonefile (it's by no means perfect and I did SOA records separately).

  64. Perl REGEXP to check primality by Daniel+Weis · · Score: 0, Redundant

    /^1?$|^(11+?)\1+$/ I like it.

  65. OK, I'll play... by PRMan · · Score: 2, Informative

    Bad filename character for Windows (if it matches, the filename is invalid):

    [*<>=+"\\/,.:;]

    E-mail (use case insensitive):

    ^\s*[\w-~&$+']+(\.[\w-]+)*@(?<domain>[\w-]+\.)+(?<tld>[0-9]{1,3}|aero|arpa|biz|com|coop|edu|gov|info|int|museum|net|org|[a-z]{2})\s*$

    GUID (use case insensitive):

    ^\{?[0-9a-f]{8}-?([0-9a-f]{4}-?){3}[0-9a-f]{12}\}?$

    IP on local private network:

    ^127\.|^10\.|^192\.168\.|^172\.1[6-9]\.|^172.2\d\.|^172.3[01]\.|^169\.254

    Removes .NET named capture syntax so that a .NET Regex string can be used elsewhere (such as Javascript) (replace with nothing):

    \?\<\w+\>

    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...

    --
    Peter predicted that you would "deliberately forget" creation 2000 years ago...
    1. Re:OK, I'll play... by tirerim · · Score: 1

      You seem to be forgetting .mil. Of course, with the new sale of TLDs, it's rapidly going to become impossible to keep track of them all; I would already just use [[:alpha:]]{2,4}|museum, and if they start having more long ones, I'll give up and do [[:alpha:]]{2,}.

    2. Re:OK, I'll play... by sr180 · · Score: 1

      Further to this, places such as the UK and NZ use .co.uk and .co.nz instead of the com tld. Which means the above regex will also fail.

      --
      In Soviet Russia the insensitive clod is YOU!
    3. Re:OK, I'll play... by donatzsky · · Score: 1

      Here's the proper way to do email validation:
      http://code.iamcal.com/php/rfc822/

  66. Slashdot by Tronks · · Score: 0

    Regex for this site: /\/\./

  67. Sed One-Liners by nevermore94 · · Score: 1

    Sed is more than just RegEx, but this is the handiest collection of sed regular expressions that I have ever found:
    http://sed.sourceforge.net/sed1line.txt

    --
    Nevermore.
  68. valid utf-8 string by Danny+Rathjens · · Score: 2, Interesting

    Here is the crazy regex to detect a valid UTF-8 string. :)
    /^(
         [\x09\x0A\x0D\x20-\x7E]            # ASCII
       | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
       |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
       | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
       |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
       |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
       | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
       |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
      )*$/x

    This can crash perl if the string being checked is too big. :D
    So it's usually better to just let perl attempt to decode anything non-ascii as utf8 and see if it fails or not. (And hope all the utf8 parsing exploits have been fixed :)
    eval { $param = decode( 'utf8', $param, Encode::FB_CROAK) if $param =~ /[^\x00-\x7E]/ };
    $param = decode( 'iso-8859-1', $param, Encode::FB_CROAK) if $@; # utf8 decode of non-ascii text failed so treat as latin1

  69. (Useful) Stupid useless articles by Kent+Recal · · Score: 3, Insightful

    Dear slashdot editors,

    slashdot.org is not stackoverflow.com.
    The articles and discussions here are not searchable in a sane way. Your recent attempts to mimic stackoverflow are just a waste of everybody's time because all those little tidbits that people post get lost in the internet noise immediately.

    We know you're bit desperate for traffic these days. But this is not the way to go.

    1. Re:(Useful) Stupid useless articles by hrimhari · · Score: 1

      I've got to agree that the "Stupid useless" series comes short to what it proposes. I've been reading them with a mix of curiosity and hope and have found the /. comment system to be, while entertaining, unfit as a reference of any kind.

      The mentioned comparison (stackoverflow) brings up an interesting idea of sorting the comments by score rather than just post date, but that link isn't so much better a reference either.

      Now, regarding Alexa, I find it hardly reliable to measure traffic, since it depends on users having such a toolbar installed. I'm sure that /. got its own stats, hence FAQ --> About Slashdot --> "How much traffic does Slashdot serve?". Would be nice to be able to see them published somewhere on the site with something like MRTG.

      --
      http://dilbert.com/2010-12-13
    2. Re:(Useful) Stupid useless articles by Anonymous Coward · · Score: 0

      Feel free to find another story or site to visit. By the comments on these four stories, many users have been enjoying them. I'm sorry you are not among those folks, but this is Slashdot: there be nerds here.

    3. Re:(Useful) Stupid useless articles by Anonymous Coward · · Score: 0

      I don't read stackoverflow. I'm not going to read stackoverflow. Your recent advertisements of stackoverflow are a much bigger waste of everyone's time than this (enormously popular) series of articles could ever be.

      The real waste of space articles on slashdot are flamewars and product advertisements.

    4. Re:(Useful) Stupid useless articles by daffmeister · · Score: 1

      Is that you Joel?

    5. Re:(Useful) Stupid useless articles by Akzo · · Score: 0

      How to look like a dick by promoting your website. Should write a book, not that anyone would read it.

      --
      Sig is for Signature, so you don't have to manually sign every post.
    6. Re:(Useful) Stupid useless articles by Kent+Recal · · Score: 1

      Just to clarify for everyone who asked: No, I'm not Joel and I'm not affiliated with S.O. in any way. Heck, I'm not even a big fan of that site.
      I linked to them to make it clear, even to people that don't know S.O., what a blatant and poor attempt of "jumping on the fad" these "Useful Stupid"-articles on slashdot are.

      It's painful to see such cheap traffic pushing tactics on what used to be the #1 "News for Nerds" site.

  70. ps | grep, without finding the grep by FlameWise · · Score: 1

    I regularly do something like

    $ ps -augwwx | grep per[l]

    with an extra [] pair around some part of the grep expression, so it doesn't find the grep command.

  71. Regex to get info from these comments by galactic-ac · · Score: 1

    Lots of points to anyone who can write the regular expression which returns the useful patterns from all these comments while filtering all the other chatter.

  72. Re:Your re is overrated by Anonymous Coward · · Score: 0

    Why don't crappy posts get modded down? A post gets torn apart in 30 replies yet stays +n Insightful?

  73. Check if a number is prime by Cow+Jones · · Score: 1
    You can use this expression (example uses Perl syntax) to test if the integer $n is a prime number:

    ("1" x $n) !~ /^1?$|^(11+?)\1+$/

    Backreferences are fun!

    CJ

    --

    Ah, arrogance and stupidity, all in the same package. How efficient of you. -- Londo Mollari
    1. Re:Check if a number is prime by mzs · · Score: 1

      Hmm it's not working for me, I am getting a string of 1s.

    2. Re:Check if a number is prime by Cow+Jones · · Score: 1

      Try this:

      sub is_prime {
      return (("1" x shift) !~ /^1?$|^(11+?)\1+$/) ? "prime" : "";
      }

      for my $i (1..30) {
      printf "%3d: %s\n", $i, is_prime($i);
      }

      CJ

      --

      Ah, arrogance and stupidity, all in the same package. How efficient of you. -- Londo Mollari
  74. One for awk by whitroth · · Score: 1

    My fav: gsub(/^ +| +$/, "", string);

    No more leading or trailing blanks in one swell foop.

            mark

  75. Readable regexes in Java by Anonymous Coward · · Score: 0

    In Java I use Hamcrest Text Patterns to make regexes more readable.

    http://hamcrest-text-patterns.googlecode.com

  76. trim whitespace from patches by smoser · · Score: 1

    If you've ever used git-am, and get patches from people that have whitespace at the end of the line, it will complain at you like:
    $ git-am some.mbox
    .dotest/patch:10: trailing whitespace.
    Requirements
    warning: 1 line adds whitespace errors
    You can fix those patch files with:
    /^\(+.\{-}\S*\)\s\+$/\1/
    I use this in vim (:%s), but probably 'sed -i' would work also.

  77. Zero-width assertions. by jonadab · · Score: 1

    My personal favorite regex trick is the zero-width assertion. I'm particularly fond of zero-width negative lookbehind assertions. Backreferences are also cool.

    --
    Cut that out, or I will ship you to Norilsk in a box.
  78. The dot by FreeFull · · Score: 0

    . Will find everything for you.

    --
    No ascii art.
  79. too easy by Anonymous Coward · · Score: 0

    [\d]{1,12}(\.\d{0,2})?([\+\-\*\/]{1}[-]?[\d]{1,12}(\.\d{0,2})?)*

    a max 12 digits long number followed by optional decimal digits and the possibility to *,/,+,- any number...

    all the best,
    Xyon

  80. Block CCs to users at same domain by HTH+NE1 · · Score: 1

    A problem with a topic for regex examples is the Lameness Filter as it sees most regular expressions as having "'junk' characters". Prefixing the regexes with tabs in ecode tags seems to have gotten around it this time.

    When used with procmail regex-filtering on the To and Cc headers, these rules match any e-mail carbon-copied to any user at example.com except user@example.com, including Bcc's that don't name only user@example.com in the To or Cc header. I find this to be effective for trapping a lot of domain-blanketing spam. Of course, this does mean you not caring about receiving e-mail shared with anyone else at your ISP. False positives are avoided by using neither a large nor local ISP.

    # Match addresses that aren't user@example.com (this line is here for formatting reasons only)
        ( "[^"]*")? <?[[:alnum:]_.]{1,3}@example\.com>?
        ( "[^"]*")? <?[[:alnum:]_.]{5,}@example\.com>?
        ( "[^"]*")? <?[^u]...@example\.com>?
        ( "[^"]*")? <?.[^s]..@example\.com>?
        ( "[^"]*")? <?..[^e].@example\.com>?
        ( "[^"]*")? <?...[^r]@example\.com>?

    The first two rules you adjust for the length of your actual username. The 3 should be one less than the length of your username and the 5 one more than that same length. The result is that it matches any names that are too short, too long, or don't have the right letters in the right positions.

    I have another that matches anything that is to user@example.com but only if the quoted name contains any characters not in the user's full name. E.g. if the user's name is "User Name", \"[^"]*[bcdf-lopqt-z][^"]*\" <user@example\.com> won't let messages to "Sergey" <user@example.com> through.

    If you're using case-sensitive regex matches, these would need to be augmented.

    --
    Oh, say does that Star-Spangled Banner entwine / The myrtle of Venus with Bacchus's vine?
  81. I concur, J-edit ROCKS! by DRAGONWEEZEL · · Score: 1

    It's x-platform, Simple to use, super easy to "see" how it works. It feels good, and what's more? It does the fricken' laundry too!

    --
    How much is your data worth? Back it up now.
    1. Re:I concur, J-edit ROCKS! by not+already+in+use · · Score: 1

      It does the fricken' laundry too!

      Ultimately, that's what sold me. The dry cleaning plugin is fantastic.

      --
      Similes are like metaphors
  82. Linguistic fun by tgv · · Score: 1

    You can do all sorts of word puzzle games with regexps, provided you've got a decent word list to begin with. E.g., find all words that do only contain vowels: ^[aeiou]+$ (this is of course language dependent and in English would fail to recognize the -y- as a vowel, but that's not necessary in this case). Or try to find good names for that killer app you're developing. Say you call it Super Video Program. Then you could search for words containing the letters s.*v.*p.* and come up with "silver plate". Ok, not a great example, but you know what I mean. Or you want to spell "access" differently. We know that a and e can sometimes be interchanged; x, cc, ks, xc, and cs; and s, ss and th. Then you come up with a regexp like '^[ae](x|cc|ks|cs|xc|xs|qs)[ae](s|ss|th)$' and find access, axes, excess, and exes.

    And you can search for those impossible entries in cross words, for which you will only need ^, . and $.

  83. How about your mom? by Rinisari · · Score: 0

    Here's a regex that will identify your mom: /^'s[:space:][mM]om$/

  84. When to use regular expressions by adamruck · · Score: 1

    I'd just like to add my two cents in here. If you are writing a quick and dirty one time script, go nuts with your regex. If your writing something that is going to be used long term, please for the sake of the maintainers just use string functions.

    It is a hell of a lot easier to redo one string function than redo a complete regex when the data format changes. Also if you use string functions you can actually do real error reporting when things don't work the way you expect them to.

    --
    Selling software wont make you money, selling a service will.
  85. The most useful regex there is! by ShatteredArm · · Score: 4, Funny

    I came up with a Regex that can be used to match literally anything (yes, anything!). It is, therefore, the most flexible regex ever concocted. Here it is:

    .*

    1. Re:The most useful regex there is! by Lord+Ender · · Score: 2, Funny

      I was able to simplify your regex somewhat so that it still matches everything, but takes up half the space:

      .

      --
      A slashdotter who didn't build his own computer is like a Jedi who didn't build his own lightsaber.
    2. Re:The most useful regex there is! by Atario · · Score: 1

      Depending on which regex flavor you're using, the empty string matches everything. Here, look:

      --
      "A great democracy must be progressive or it will soon cease to be a great democracy." --Theodore Roosevelt
    3. Re:The most useful regex there is! by Anonymous Coward · · Score: 0

      And it also matches nothing!

      Dude - that's deep.

    4. Re:The most useful regex there is! by trb · · Score: 1
      I find that simple regexes involving dot or dot-star are some of the handiest, especially when editing or reading code with a pager. For example:

      1) Let's say you're editing a C program, and you need to search for a string:
      dev->q->item

      who wants to type all those ->'s in a search?

      instead, search for
      dev..q..item

      Same goes for searching for a path. If you're searching for foo/bar/arp , don't bother typing /foo\/bar\/arp , instead, type /foo.bar.arp .

      When I'm searching, I'll often hit a dot rather than any key I need to hit shift for or that's hard to find or that will complicate the regex (like any of ~|/\). Yes, I know you need to type the real characters when you're actually typing the code, but this is about matching and searching.

      2) In editors and pagers (like less), all matched strings on the display are often highlighted (rather than just the one you found). It's handy to use this highlighting to break text up visually when you're reading it.

      Let's say you're doing a recursive diff of two directories and there are a handful of files with changes, and you want to look at them. you do: "diff -r dir1 dir2" and a bunch of changes scroll down the screen, so you pipe into less, and it's still a big mass of text. Some horizontal divisions might make it easier to read.

      The diff output will have the string "diff -r dir1/foo dir2/foo" before each file foo.

      If you search for the string diff or ^diff , less will highlight all the matched strings. But if you search for the string ^diff.* , then less will highlight the whole line found, which is very handy when you're parsing the file visually

      3) When editing, if you have a long messy string to search for, just find something useful that the beginning and end of the string to search for. If you need to search for internationionlization, search for int.*ion. If it matches something else, you just hit a key or two to search again for the next one.

      4) When browsing text in a file, many people just lean on the arrow keys to navigate around. I think it's always handier to search for a string rather than just blindly scrolling. And make the search simple, and use the "search next (or last)" keys to move around. Don't be too specific. Be lazy (or fuzzy or efficient). Regexes need to be precise if you are coding a task and you need to find data exactly. When you're editing, you're just using them to navigate, and you can be fuzzier.

  86. Real world email address validation by hm2k · · Score: 1

    After a whole bunch of research I ended up with this for email validation:

    /^[\w!#$%&\'*+\/=?^`{|}~.-]+@(?:[a-z\d][a-z\d-]*(?:\.[a-z\d][a-z\d-]*)?)+\.(?:[a-z][a-z\d-]+)$/iD

    Note: the modifiers are designed to work with PHP.

    Source: http://www.hm2k.com/posts/what-is-a-valid-email-address

  87. I had a really great trick but... by IrishLimey · · Score: 0

    I kept getting the response "Filter error: Please use fewer 'junk' characters."

    Is there anyway to turn this off or at least bypass the filter, as regular expressions are just made up of 'junk' characters.

  88. (Useful) Stupid Vista Tricks by Anonymous Coward · · Score: 0

    I wonder if askslashdot would result in an article on that? Let the +5 Funnies flow...

  89. del *.[ch] by gatkinso · · Score: 1

    Usually works for me. :P

    --
    I am very small, utmostly microscopic.
    1. Re:del *.[ch] by mzs · · Score: 1

      That is glob not a regex, plus it is for command.com and del is doing the globbing. It would be rm for a real shell. Also depends on the version if brackets work for MS.

  90. nice complex BGP routing regex by funkboy · · Score: 1

    I like this regex evaluator for testing things out: http://www.cuneytyilmaz.com/prog/jrx/
    Really useful.

    Someone here once made a regex to filter out nonauthorized BGP communities from peer announcements. It was three lines long.

    Really slick asking people to post regexes on slashdot and then having all the posts get rejected due to "junk" characters"...

  91. two bees by x78 · · Score: 1

    m/(2b)?/

    One of the better ones :)

    --
    Don't panic
  92. Re:email validation... FAIL by jeremyp · · Score: 3, Insightful

    Your regex doesn't allow + signs in the name part.

    Nor, I would suspect would it handle quoted strings e.g. "Jeremy P"@example.com is technically a valid RFC 822 address.

    And having just looked up the RFC 5322 spec which you quote, I see there are more cases you fail to take acount of e.g.

    Jeremy P <jeremyp@example.com>

    Also, what makes you think upper case in domain names is invalid? jeremyp@example.COM fails validation.

    --
    All I want is a secure system where it's easy to do anything I want. Is that too much to ask ~~ Randall Munroe
  93. A "stupid" trick? by fgaliegue · · Score: 1

    By Jeffrey Friedl's "Mastering Regular Expressions", O'Reilly. Read it from page 1 to the end until you understand.

    Enough said.

  94. Universal RegEx by pablodiazgutierrez · · Score: 1

    ".*"

    It never misses.

  95. Useful regex for find by Anonymous Coward · · Score: 0

    Most useful regex ever: .*your\ base.*

    Used in:

    find / -iregex .*your\ base.* -exec chown us:us {} \;

  96. the greatest regex trick I ever learned by Anonymous Coward · · Score: 0

    Is the following:

    (.*?)

    I don't know what to call it. I don't know how to explain what it does, programmatically. All I know is that it makes it a hell of a lot easier to capture groups. For instance! HTML href-grabbing could be written this way: /href=['"]([^'"]*)['"]/

    But it's much easier, I think, to write it this way: /href=['"](.*?)['"]/

    This probably doesn't drive home just how handy this is. Basically, any time that want to grab a pattern from a "hole" and you know what the text surrounding the hole looks like, you can drop in (.*?)

    1. Re:the greatest regex trick I ever learned by Dracorat · · Score: 1

      It's called a "non greedy" match.

      .* is "greedy" in that it will match the *most* characters it can until it can match no more.

      .*? is "non greedy" in that it will match the *least* amount of characters it can to satisfy the equation.

      And I too often forget about it and write hellacious regexes only to be looking at them later and realize how much I overcomplicated it.

  97. In best Homer Simpson voice by Anonymous Coward · · Score: 0

    Hmmm regex p0rn...

  98. How about my killfile? by mbourgon · · Score: 1

    I frequently look for tv episodes on the net, and find myself killfiling shows thusly:
    S[0-9][0-9]E[0-9][0-9] , which would filter something like friends.s05e10.[etc]

    Is there a smaller way? The program I use is case-insensitive, but the only shorter way I can come up with "S[0-9]+E[0-9]+" would risk finding other sets inadvertently. (or would it?)

    Any suggestions?

    --
    "Sometimes a woman is a kind of religion, she can save your soul & set you free from all your sins" - Bad Examples
    1. Re:How about my killfile? by isli · · Score: 1

      /s\d{2}e\d{2}/i

    2. Re:How about my killfile? by mbourgon · · Score: 1

      Clever. Twelve characters for the part I need. Thanks, I'll test that vs s[0-9]+e[0-9]+ (14 characters).
      Now to go read a regex manual so I know _how_ it works.

      --
      "Sometimes a woman is a kind of religion, she can save your soul & set you free from all your sins" - Bad Examples
  99. prime numbers by Anonymous Coward · · Score: 0

    perl -wle '$_ = 1; (1 x $_) !~ /^(11+)\1+$/ && print while $_++'

  100. Use another char instead of / by Anonymous Coward · · Score: 0

    Not really related to regular expressions, but sed, vim, maybe more, accept instead of a / also other signs which is easy when a / must be matched:
    sed sx/home/foox/home/barx

  101. It is called by Anonymous Coward · · Score: 0

    a back reference. More at

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

    I use java for programming, know nothing of Perl. In java it works fine both in regexes and replacement expressions. (I think you could try to to escape your last backslash?) Here is a regex I use to find words repeated within 30 chars.

    String DOUBLE_WORD_DISTANCED_REGEX =
    "(?ix) # Turn case insensitive and comments on\n" +
    "\\b # Start with a boundary\n" +
    "([^\\s\\p{Punct}]++) # Read somthing that is not whitespace or punctuation\n"+
    "(?:\\s++) # Read - don't capture - something that is whitespace\n" +
    "(?:[^.,!?:;]{1,30}) # Read - don't capture - something that is not punctuation \n" +
    "(?:\\s+) # Read - don't capture - something that is whitespace\n" +
    "(\\1) # Repeat the word\n" +
    "\\b # End with a word boundary\n";

  102. Regex and prime numbers by creative_Righter · · Score: 1

    Given a string of 1's, matches if the length of the string is non-prime:

    /^1?$|^(11+?)\1+$/

    (This is the blog post http://weblog.raganwald.com/2008/02/so-you-think-you-know-regex-fu.html)

    Took me a while to figure out how this works. This is the sort of thing that makes me smile.

  103. Re:Your re is overrated by Zoolander · · Score: 1

    I blame Bush.

    --
    Meep.
  104. Well, I Had a Cool Trick... by Maltheus · · Score: 1

    ...but ./ wouldn't let me post it cause it had to many "junk" characters. I guess one man's junk is another mans regex, but it didn't seem more complicated than anyone else's expressions.

  105. Winky by Anonymous Coward · · Score: 0

    /^.*/

  106. DOS - Batch File Executable for Regex? by Anonymous Coward · · Score: 0

    Ok, as the crafter of more than a few DOS batch files, I frequently like using regex expressions to make my scripts do neat things. What Regex command line applications can the Slashdot community recommend? Which ones have the most flexibility?

    Thanks

  107. WTF? by Anonymous Coward · · Score: 0

    Is this hi5 now?

  108. parsing text into a template by jms1 · · Score: 1

    this may be more about perl than regular expressions in general, but i've been using a function for several years to insert data into templates. the template contains tags like "${name}", the function is called with the template and a list of substitutions like ("name" => "john", "ip" => "127.0.0.1") and the return value is the "filled out" version of the template.

    sub parse_string($;@)
    {
        my $string = shift ;
        my %rep = @_ ;

        $string =~ s/\$\{(.+?)\}/$rep{$1}/gms ;
        return $string ;
    }

  109. Stupid$1Tricksdot$2 by mebrahim · · Score: 1

    s/\(Useful\) Stupid (.*) Tricks(\??)/Slashdot is getting Stupid$1Tricksdot$2/

  110. It must be said by IorDMUX · · Score: 3, Funny

    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

    -- Jamie Zawinski

    --
    >> Standing on head makes smile of frown, but rest of face also upside down.
  111. Speaking of one-liners (was Re:Ignore whitespace) by shtrom · · Score: 1

    > One liners are for show, not for actual usage.

    Mostly true. But it's always a bit of a perverse sense of satisfaction when you come out with an obscure one-liner that does exactly what you want.

    Besides, I indulge myself in that way too much these days. It turns out that reading completely illegible one-liners is mostly a matter of habit.

    Sometimes, I scare myself.

    On a very slightly different matter, I always keep http://sed.sourceforge.net/sed1line.txt closeby, it has proven useful more than once.

  112. Useful for home-grown templating by Anonymous Coward · · Score: 0

    This is quick-and-dirty perl (you did say stupid, right?) -- could be done better with something like template toolkit:

    $ cat in.file
    eval($goodbye = "hello")
    You say goodbye and I say: $goodbye

    $ cat xx.pl
    #!/bin/perl
    open(INFILE,"in.file");
    while(<INFILE>) {
        print "Before regex: ";
        print;
        if ( /eval\s?(.*)??/ ) {
            s/eval\s?\((.*)??\)/$1/g;
            eval;
        } else {
                    s/(\${?\w*}?)/$1/eegs;
                    print "After regex : ";
                    print;
        }
    }

    $ perl ./xx.pl
    Before regex: eval($goodbye = "hello")
    Before regex: You say goodbye and I say: $goodbye
    After regex : You say goodbye and I say: hello

    --

  113. Low UIDs by Doc+Hopper · · Score: 1

    What's it take to be considered a low UID?

    1. Re:Low UIDs by Daengbo · · Score: 1

      Fewer than seven digits these days .... ;)

  114. Opposite by Christopher_Olah · · Score: 4, Insightful

    IMHO, this is exactly the way that Slashdot should be going. Threads like this are interesting, add to the reservoirs of internet knowledge, and have the highest quality to noise ratios.

    I (and I suspect many others) read Slashdot not for the latest +5 funny comment (though those can be fun to read) but to read the opinions of brilliant minds. And when those minds start trading secrets... Everyone wins.

  115. Useful regex tricks? by famebait · · Score: 1

    No such thing. Stupid? Sure. Lotsofem.
    But to be conisdered a trick and not just a normal use of the syntax, it is by defenition a case where you should really be writing a parser. It's not that hard, and a helluva lot easier to discover and fix bugs in.

    I love regexes too. Where they're useful, which is for automating tedious but trivial matcheing. But they have done no end of harm to the competency of the average geek by allowing them to indefinitely postpone learning how to writr a simple parser, which is really something every programmer should know. I'm not even talking yacc/lexx (but yous hould know when to go read up on that too), even just a simple char by char or tokeniser based little state thing will sort out a lot of stuff in a clear, simple and above all understandable way that would require a totally unreadable mess of a rfegex that probably contains five bugs you will never spot.

    Sure, it might take a page of code in stead of code in stead of a 128-long line, but you will be well into understanding it and customising it to your needs before you're even halfaw reading that monster regex.

    Yes, I can write those too if I have too, and have, but it about as mainainable as self-modifying machine code. The complex cases are simply lousy at explaining what they do, and in most cases less efficient that a well written string parsiing routine. If you haven't grown out of the illusion that compact code is faster to execute or even code (let alone maintain), you probably can't write a foolproof regex for a complex syntax anyway.

    --
    sudo ergo sum
  116. Re:Your re is overrated by ThinkTwicePostOnce · · Score: 1

    I have a logical answer. It looks good at first. You'd have to go back after seeing all
    the tearing apart, which is a bother. But also, if you DO bother to go back after seeing
    it shredded, and mod it down then, the meta moderators (who judge your modding) don't see all
    that shredding either. So you look like a jerk who modded down a perfectly good-looking-at-first
    post.

    And finally the overriding question, a post that looks good at first and is then shredded
    can be a vital trigger for an interesting discussion (the shredding itself).

    --
    Hide all sigs: Click HELP+Prefs (top), VIEWING (last on right), DISABLE SIGS (3rd on left) and SAVE (hidden at bottom).
  117. Negative and Positive Lookaheads by Techman83 · · Score: 1

    I use these a lot for several RSS feeds and Torrentflux ;)

    Negative Lookahead -> Match only those that exclude the string

    (?!.*?720p)

    Positive Lookahead -> Match only those that include the string

    (?=.*PDTV)

    Eg

    (?=.*PDTV)(?!.*720p)^Non.Copyrighted.TV.Show.s01e[\d].*

    Can add as many positive/negative look aheads as you want, very handy for weeding out the half dozen copies you'd get of each one!

    --
    # cat /dev/mem | strings | grep -i cat
    Damn, my RAM is full of cats. MEOW!!
  118. Prime numbers by leipzig3 · · Score: 1

    http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ describes a way to use regex to check if a string contains a prime number of ones.
    Here is the actual regex. /^(11+?)\1+$/
    Ironically, this isn't even a "regular" (in the CS sense) language at all, which just shows how powerful regex really is.

  119. Re:Your re is overrated by scotsghost · · 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.

  120. Redundant title by kaens · · Score: 1

    They're all stupid regex tricks, however useful they may be.

  121. Better Chomp by saur2004 · · Score: 1
    I always do this in my perl scripts because I never know the format that Im going to get from our clients.

    sub betterchomp { $_[0] =~ s/[\n\r]//g; }

  122. x.255.x.x is a VALID IP address! by Anonymous Coward · · Score: 0

    I went to the U of Iowa, which has 128.255.x.x. When I worked in IT there for a while we occasionally ran into stupid software that didn't believe our IP address was a valid one (like the first couple versions of the Solaris 2.x installer)

    So please fix your regex to be [0-5] and not [0-4].

    I'm not sure whether the first octet can be 255, which you allow, but I'll leave it for someone else to correct if necessary...

    When the U of I hospitals got their own IP address range they were given 129.255, which I assume was because we'd already experienced whatever pain there was to be found with that 255 octet and knew how to work our way around it!

  123. Re:Your re is overrated by Anonymous Coward · · Score: 0

    did you know the RFC specifies leading-zero-for-octal and leading-0x-for-hex?

    yes, and I assume most other people who have studied IP at all did as well.

    if the mods do their job

    don't hold your breath.

  124. regex clothes! by mistahkurtz · · Score: 1

    c'mon, nobody remembered the handy skirt and shirt over at xkcd? skirt's regex, shirt's linux and regex.

    --
    not only is time travel possible, it's irrelevant.
  125. How about some towers of hanoi by CoolVibe · · Score: 1

    And this proves that sed is somewhat of a mongrol (but I love it all the same): http://sed.sourceforge.net/grabbag/tutorials/hanoi.htm

  126. Rexg Library by Anonymous Coward · · Score: 0

    First time posting a comment here so cut me some slack. I think that the regex library (http://regexlib.com/) is a very good source for regexs for all kinds of things.
    Most of the regexs that have been posted here can be found there.

  127. At a voice command control demo by Anonymous Coward · · Score: 0

    At a voice command control demo, one member of the audience stood up and yelled: C: ENTER DEL *.* ENTER about 6 seconds later the person demoing the SW exclaimed OMG

  128. Re:email validation... FAIL by Anonymous Coward · · Score: 0

    Email address validation is very poorly implemented on most software. Take a look at this, this is an email validation regex. You will be surprised of how complex can be:

    http://ex-parrot.com/~pdw/Mail-RFC822-Address.html

  129. For Makefiles by mzs · · Score: 1

    I often use something like this:

    .SUFFIXES : .foo .bar

    .foo.bar : ; baz -o '$(subst ','\'',$*)'.foo -- '$(subst ','\'',$<)'

    This way if I have unusual characters in filenames it handles it fine. I have unusual characters since I get document files to process from non-programmer types with 'friendly' names that contain spaces, apostrophes, parens, quotes, etc.

    You can use a similar trick with sed in bash to build a escapified commandline to later eval that keeps all the arguments nicely as one argument each and prevents the shell from interpreting special characters.

  130. ... and then they had two problems. by gr8dude · · Score: 1

    It is better to use a regexp to make sure the string is in the format X.Y.Z.W; afterwards use numerical comparison to verify that the numbers are between 0 and 255, etc. The proposed regexp is long and difficult to maintain.

  131. tabs to spaces by kisrael · · Score: 1

    This beautiful line of Perl code:
    $line =~ s/([^\t]*)\t/$1." "x(8-length($1)%8)/ge;
    replaces tabs with the appropriate number of spaces, respecting the tab stops. Its author Phiroze Parakh rocks

    --
    SO YOU'RE GOING TO DIE: The Comic for Dealing with Death
  132. Vi Transmogrifier by CTalkobt · · Score: 1

    From vi,
            1,$s/\(.\)\(.\)/\2\1/g

    will yield a copy of your file which looks disturbingly different.

    Doing the command again, will yield the original file.

    For even more confusion, try :
            1,$s/\(.\)\(.\)\(.\)/\3\2\1/g

    Repeat and rinse as necessary...

    --
    There's a gorilla from Manilla whose a fella that stinks of vanilla and has salmonella.
  133. ignoring grep in ps output by jmanning2k · · Score: 1

    In the borderline stupid category, but I find this quite useful.
    When grepping the output from ps, bracket the first character.

    ps ax | grep [s]sh

    If you don't, the grep command may show up in the results.

  134. More interesting than useful... by bmckeever · · Score: 1

    More interesting than useful, but I think the idea of writing a regex to do integer division is awesome.

    http://bmm6o.blogspot.com/2008/03/divisibility-testing-and-pattern_27.html

    --
    Your favorite .sig sucks
  135. Matching what's between things by Ruadhri · · Score: 1

    I realise nobody is still reading this, and as this is my first ever post it'll come in way below most readers' filters anyway. However, I didn't see it posted (though I'm reading at quite a high filter level too), and it's pretty useful:

    To match something between quotation marks, for example, do:
    m/"[^"]+"/

    Which says, match a quote, followed by at least one character which isn't a quote, followed by a quote.

    There are further refinements to this, and it doesn't allow for quotes within quotes, or for empty quotes (""), but it does what you want most of the time.

    This and other gems are to be found in Jeffrey Friedl's Mastering Regular Expressions.

  136. use regexbuddy by BlueYoshi · · Score: 1
    Really, if you not familiar or even if you read twice and understood "Mastering Regular Expressions". Use tools this is very nice: http://www.regexbuddy.com/

    Do you know other tools?

    --
    "Use cases are fairy tales..." I. S. 2005