Regex: Help w/ matching non-US country names

Discussion in 'Programming' started by mhuggins, Jul 2, 2008.

  1. #1
    I'm trying to come up with a regular expression that matches 2-character country codes. Currently, I have it so that it works for any 2 characters. However, I don't want it to match when the country code provided is "US". Can someone help me figure out what I need to change?

    $regex = '/^([a-z]{2})$/i';
    Code (markup):
     
    mhuggins, Jul 2, 2008 IP
  2. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #2
    Cant you use:

    
    $countryCode = "UK";
    
    if(strtolower($countryCode) != "us"){
    //Do Stuff
    }
    
    PHP:
    That will make the country code lowercase (so it matches any case), and if it doesn't equal "us", then it can do stuff.

    EDIT: Assumed you were using PHP, but any language should be more or less do that - take a string to lowercase, and compare it with a "does not equal" operator.
     
    blueparukia, Jul 2, 2008 IP
  3. mhuggins

    mhuggins Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    My goal is to create a regex that can be data-specific so that I don't have to make the code data-specific. As such, creating an if-condition isn't an option for me.
     
    mhuggins, Jul 2, 2008 IP
  4. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #4
    Divide the regex [a-t][v-z][A-T][V-Z] like this.
     
    it career, Jul 3, 2008 IP