(ir)Regular Expressions...help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    (ir)Regular Expressions...help!

    Kind programmers of the world:

    I've been lookign to learn python and when I tell people this they say "make sure you know RE"

    That's all well and good, and I read the tutorial but i'm wondering if anyone out there knows of a good book or good place that has intro level regular expressions. A place that is kind of like a school text book in that it'll have small tasks/questions and there'll be answers out there...

    Thanks
    PC
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by Patrick C
    Kind programmers of the world:

    I've been lookign to learn python and when I tell people this they say "make sure you know RE"

    That's all well and good, and I read the tutorial but i'm wondering if anyone out there knows of a good book or good place that has intro level regular expressions. A place that is kind of like a school text book in that it'll have small tasks/questions and there'll be answers out there...

    Thanks
    PC
    I just got Mastering Regular Expressions. It's a must have. There a tons of things that I never thought of doing with regular expressions.

    I agree: Why in the world did those unix guys ever call them "regular" expressions?

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by Patrick C
      Kind programmers of the world:

      I've been lookign to learn python and when I tell people this they say "make sure you know RE"

      That's all well and good, and I read the tutorial but i'm wondering if anyone out there knows of a good book or good place that has intro level regular expressions. A place that is kind of like a school text book in that it'll have small tasks/questions and there'll be answers out there...

      Thanks
      PC
      're' is about pattern matching. Function findall() in the re module is used to retrieve a list of substrings that match a regular expression. Here is an example from a recent thread:
      Code:
      >>> s = 'SET 102 = 1001323 THRU 1001331,1001343 THRU 1001349,11201,327745,47757,54786598'
      >>> sList = re.findall('\d+ +THRU +\d+|\d+', s)
      >>> sList
      ['102', '1001323 THRU 1001331', '1001343 THRU 1001349', '11201', '327745', '47757', '54786598']
      >>>
      First notice the vertical bar character '|'. That means 'match the expression to the left' OR 'match the expression to the right'. A regular expression can have multiple '|' characters.

      Next notice the backslash character '\'. A number of backslash sequences are defined that correspond to character sets. The sequence '\d' corresponds to the set of digits 0 through 9, and is the same as r'[0-9]'. The 'r' stands for 'raw string'.

      Notice the plus character '+'. It means match one or more occurances of the preceeding expression.

      Spelled out, the above expression matches a sequence of characters....

      one or more digits (0-9) followed by one or more spaces followed by the string 'THRU' followed by one or more spaces followed by one or more digits

      OR

      one or more digits

      I hope I have not confused you!

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by Patrick C
        .... when I tell people this they say "make sure you know RE"
        that's not very true. regexp is powerful and is used extensively in Perl,Shell tools like awk,sed and others. Its good to know regexp, but you do not have to use regexp all the time if you code in Python. This is because Python is built to easily manipulate strings using string methods and slicing/indexing etc..regexp works if you know what's going on, but if it doesn't match what you want, you won't immediately see what's going on either. if your regexp gets too complex, it tends to get harder to troubleshoot too. Its almost always more "readable" using simple Python string methods in those cases (although the code may be longer).
        that's just my $0.02.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by ghostdog74
          that's not very true. regexp is powerful and is used extensively in Perl,Shell tools like awk,sed and others. Its good to know regexp, but you do not have to use regexp all the time if you code in Python. This is because Python is built to easily manipulate strings using string methods and slicing/indexing etc..regexp works if you know what's going on, but if it doesn't match what you want, you won't immediately see what's going on either. if your regexp gets too complex, it tends to get harder to troubleshoot too. Its almost always more "readable" using simple Python string methods in those cases (although the code may be longer).
          that's just my $0.02.
          Very good points, my friend. I agree, completely.

          Comment

          Working...