1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2007
    Rep Power

    Simple Regex Not Working?

    Python Code:

    Basically, it should match anything from "i live in tucson" to "my favorite food is cheese"... with the (\w+) being the only values saved in a compiled array. I'm using Python, meaning I am going to save this into an array... but I only want to keep the two (\w+)'s in it. What am I doing wrong? I've tried everything!

    Python Code:
    lalala = "my favorite food is cheese"
    pattern = re.compile('(i|my|my\s+own|mine)\s+(\w+)\s+(in|at|is|are|am|will\s+be)\s+(\w+)')
    wildcard = pattern.match(lalala).groups()
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Pennsylvania, USA
    Rep Power
    Your regexp matches "my" then any word, then "is" then any word.

    You have TWO words between "my" and "is" in your test.

    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo