Help with regular expression

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

    Help with regular expression

    I'm trying to write an RE to match a string that might or might not be
    ther and everything past it up to another string that might or might not
    be there and everything past it to a third string that might or might
    not be there and everything past it.

    Say my strings are "STRING1", "STRING2", and "String3".

    Would the re be:

    r'((STRING1.*)( STRING2.*)(STRI NG3.*))'

    The goal is to separate the groups by newlines, but I only want the
    first group to match up to the second group and the second to match up
    to the third.

    Normally I would achieve this by typing:

    pat = re.compile(r'(( STRING1.*)(STRI NG2.*)(STRING3. *))')
    m = pat.findall(s)
    s = '\n\n'.join(m[0])

    but the first group would seem to match everything.

    How can I get it to do what I want?

    -- Stephen

  • anton muhin

    #2
    Re: Help with regular expression

    Stephen Boulet wrote:
    [color=blue]
    > I'm trying to write an RE to match a string that might or might not be
    > ther and everything past it up to another string that might or might not
    > be there and everything past it to a third string that might or might
    > not be there and everything past it.
    >
    > Say my strings are "STRING1", "STRING2", and "String3".
    >
    > Would the re be:
    >
    > r'((STRING1.*)( STRING2.*)(STRI NG3.*))'
    >
    > The goal is to separate the groups by newlines, but I only want the
    > first group to match up to the second group and the second to match up
    > to the third.
    >
    > Normally I would achieve this by typing:
    >
    > pat = re.compile(r'(( STRING1.*)(STRI NG2.*)(STRING3. *))')
    > m = pat.findall(s)
    > s = '\n\n'.join(m[0])
    >
    > but the first group would seem to match everything.
    >
    > How can I get it to do what I want?
    >
    > -- Stephen
    >[/color]

    Try non-greedy wildcards: r'((STRING1.*?) (STRING2.*?)(ST RING3.*?))'
    - - -

    should do the trick.

    hth,
    anton.

    Comment

    • Ulrich Petri

      #3
      Re: Help with regular expression

      "anton muhin" <antonmuhin.REM OVE.ME.FOR.REAL .MAIL@rambler.r u> schrieb im
      Newsbeitrag news:bkbssb$102 f$1@news.peterl ink.ru...[color=blue]
      > Stephen Boulet wrote:
      >[color=green]
      > > I'm trying to write an RE to match a string that might or might not be
      > > ther and everything past it up to another string that might or might not
      > > be there and everything past it to a third string that might or might
      > > not be there and everything past it.
      > >[/color]
      >
      > Try non-greedy wildcards: r'((STRING1.*?) (STRING2.*?)(ST RING3.*?))'
      > - - -
      >[/color]

      Since you said that each of the three strings might or not be there you
      would need:

      r'((STRING1.*?) ?(STRING2.*?)?( STRING3.*?)?)'

      Ciao Ulrich


      Comment

      Working...