Concerning Regular Expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tempo

    #1

    Concerning Regular Expressions

    I've been reading a bunch of articles and tutorials on the net, but I
    cannot quite get ahold of the whole regular expression process. I have
    a list that contains about thirty strings, each in its own spot in the
    list. What I want to do is search the list, say it's called 'lines',
    for 'R0 -'.

    Thanks in advanced for any and all info that I recieve.


    -Tempo-

  • Tempo

    #2
    Re: Concerning Regular Expressions

    Whoops. I've got another tid-bit to tack onto this post. What kind of
    value does this expression return:

    re.sub(r'^R0 -', line)


    Does it return a '1' if successful and a '0' if not successful?

    Comment

    • Alex Martelli

      #3
      Re: Concerning Regular Expressions

      Tempo <bradfordh@gmai l.com> wrote:
      [color=blue]
      > I've been reading a bunch of articles and tutorials on the net, but I
      > cannot quite get ahold of the whole regular expression process. I have
      > a list that contains about thirty strings, each in its own spot in the
      > list. What I want to do is search the list, say it's called 'lines',
      > for 'R0 -'.[/color]

      No need for regular expressions for this task as you expressed it:

      for aline in lines:
      if 'R0 -' in aline:
      doyourworst(ali ne)

      I've sweated substantial amount of blood and other bodyfluiids trying to
      explain "the whole regular expresson process" concisely and effectively
      in chapter 9 of "Python in a Nutshell" (and I'm going through the
      heartache of preparing the second edition now -- let's say I'm currently
      grasping for any straw to avoid the work;-) -- if I didn't do a good
      enough job there, I sure can't do better here. But one thing stands: if
      you don't yet understand fully the abilities of Python's strings and
      other builtins, as for example in the above snippet, studying REs is
      premature. WAY premature.


      Alex

      Comment

      • Terry Reedy

        #4
        Re: Concerning Regular Expressions


        "Tempo" <bradfordh@gmai l.com> wrote in message
        news:1138595614 .254959.70740@o 13g2000cwo.goog legroups.com...[color=blue]
        > Whoops. I've got another tid-bit to tack onto this post. What kind of
        > value does this expression return:
        >
        > re.sub(r'^R0 -', line)
        >
        >
        > Does it return a '1' if successful and a '0' if not successful?[/color]

        Start up an interactive interpreter window, try it, and see.
        Such discoveries are one thing the interactive mode is designed for.




        Comment

        • Tempo

          #5
          Re: Concerning Regular Expressions

          You are right that my move towards regular expressions was way
          premature, but also this post may too turn out to be a little
          premature. I guessed and checked myself a way to accomplish what I
          needed and I will include it in this post. But first Alex (doesn't have
          to be Alex) could you tell me if your snipplet and mine would be near
          perfect subsitutes for one another? I believe they accomplish the same
          task of looking for 'R0 -' in the list 'lines', however, as you have
          guessed, I do not know my way around Python very well yet. Here is my
          snipplet:

          log = open('C:\log_0. txt')
          lines = log.readlines()
          import re
          for line in lines:
          R = re.search(r'^R0 ', line)
          if R != None:
          n = 1
          print n
          import time
          time.sleep(3)
          log.close()

          Comment

          • Alex Martelli

            #6
            Re: Concerning Regular Expressions

            Tempo <bradfordh@gmai l.com> wrote:
            [color=blue]
            > You are right that my move towards regular expressions was way
            > premature, but also this post may too turn out to be a little
            > premature. I guessed and checked myself a way to accomplish what I
            > needed and I will include it in this post. But first Alex (doesn't have
            > to be Alex)[/color]

            Uh, I think I DO have to be Alex, sorry. It's sort of, my job
            description, if you will...
            [color=blue]
            > could you tell me if your snipplet and mine would be near
            > perfect subsitutes for one another? I believe they accomplish the same
            > task of looking for 'R0 -' in the list 'lines', however, as you have
            > guessed, I do not know my way around Python very well yet. Here is my
            > snipplet:
            >
            > log = open('C:\log_0. txt')
            > lines = log.readlines()
            > import re
            > for line in lines:
            > R = re.search(r'^R0 ', line)
            > if R != None:
            > n = 1
            > print n
            > import time
            > time.sleep(3)
            > log.close()[/color]

            Your re.search looks for R0 at the beginning of a line, and there is no
            hint whatsoever of 'R0 -'. I.e., your loop here is more like:

            for line in lines:
            if line.startswith ('R0'):
            print 1

            etc. I won't even guess what the stuff AFTER the loop means;-).


            Alex

            Comment

            Working...