Regular Expression Syntax Help

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

    #1

    Regular Expression Syntax Help

    I'm trying to get the right syntax for my regular expression. The
    string I'm trying to parse is:

    # myString
    [USELESS DATA]
    Name: David Dude
    [USELESS DATA]

    Right now, I'm using the following code:


    pattern_Name= '''(?x)
    Title:\s+(\w+)\ s+
    '''
    names = re.findall(patt ern_Name, myString)
    print names

    This prints out a list containing only the first names. I want to
    search the string until it finds a '\n' endline, but I experimented
    with that, and couldn't find what I need.

  • Ernesto

    #2
    Re: Regular Expression Syntax Help

    The word "Title" there should be "Name".

    What I really need is the pattern for getting the entire string after
    "Name: " until a '\n' is found.

    Comment

    • Raja Raman Sundararajan

      #3
      Re: Regular Expression Syntax Help

      try this. maybe this is what you want?

      reg = re.compile('Nam e:.*\\n', re.IGNORECASE)

      Comment

      • Ernesto

        #4
        Re: Regular Expression Syntax Help

        So now I need to add the requirement that only "Name:" 's which are
        followed by

        "Request: Play" or "Request: Next" ANYWHERE between the previous titles
        and the new titles. Can I use RE's for that ?


        Ernesto wrote:[color=blue]
        > I'm trying to get the right syntax for my regular expression. The
        > string I'm trying to parse is:
        >
        > # myString
        > [USELESS DATA]
        > Name: David Dude
        > [USELESS DATA]
        >
        > Right now, I'm using the following code:
        >
        >
        > pattern_Name= '''(?x)
        > Title:\s+(\w+)\ s+
        > '''
        > names = re.findall(patt ern_Name, myString)
        > print names
        >
        > This prints out a list containing only the first names. I want to
        > search the string until it finds a '\n' endline, but I experimented
        > with that, and couldn't find what I need.[/color]

        Comment

        • Raja Raman Sundararajan

          #5
          Re: Regular Expression Syntax Help

          Oh! yes you can use re for that.
          You just need to change the pattern a bit

          I did not understand where the "title" will be so I have ignored it,
          but I got something below which will be helpful for you
          [color=blue][color=green][color=darkred]
          >>> value = """name:asasasa sas\nrequest: play\ntitle"""
          >>> reg = re.compile('Nam e:.*\\nrequest: .....', re.IGNORECASE)
          >>> re.findall(reg, value)[/color][/color][/color]
          ['name:asasasasa s\nrequest: play'][color=blue][color=green][color=darkred]
          >>> value2 = """name:asasasa sas\nrequest: next\ntitle"""
          >>> reg = re.compile('Nam e:.*\\nrequest: .....', re.IGNORECASE)
          >>> re.findall(reg, value2)[/color][/color][/color]
          ['name:asasasasa s\nrequest: next'][color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          Comment

          Working...