re Insanity

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

    #1

    re Insanity

    For some reason, I am having the hardest time doing something that should
    be obvious. (Note time of posting ;)

    Given an arbitrary string, I want to find each individual instance of
    text in the form: "[PROMPT:optional text]"

    I tried this:

    y=re.compile(r' \[PROMPT:.*\]')

    Which works fine when the text is exactly "[PROMPT:whatever]" but
    does not match on:

    "something [PROMPT:foo] something [PROMPT:bar] something ..."

    The overall goal is to identify the beginning and end of each [PROMPT...]
    string in the line.

    Ideas anyone?
    --
    ----------------------------------------------------------------------------
    Tim Daneliuk tundra@tundrawa re.com
    PGP Key: http://www.tundraware.com/PGP/

  • Fredrik Lundh

    #2
    Re: Insanity

    Tim Daneliuk wrote:
    [color=blue]
    > Given an arbitrary string, I want to find each individual instance of
    > text in the form: "[PROMPT:optional text]"
    >
    > I tried this:
    >
    > y=re.compile(r' \[PROMPT:.*\]')
    >
    > Which works fine when the text is exactly "[PROMPT:whatever]"[/color]

    didn't you leave something out here? "compile" only compiles that pattern;
    it doesn't match it against your string...
    [color=blue]
    > but does not match on:
    >
    > "something [PROMPT:foo] something [PROMPT:bar] something ..."
    >
    > The overall goal is to identify the beginning and end of each [PROMPT...]
    > string in the line.[/color]

    if the pattern can occur anywhere in the string, you need to use "search",
    not "match". if you want multiple matches, you can use "findall" or, better
    in this case, "finditer":

    import re

    s = "something [PROMPT:foo] something [PROMPT:bar] something"

    for m in re.finditer(r'\[PROMPT:[^]]*\]', s):
    print m.span(0)

    prints

    (10, 22)
    (33, 45)

    which looks reasonably correct.

    (note the "[^x]*x" form, which is an efficient way to spell "non-greedy match"
    for cases like this)

    </F>



    Comment

    • Duncan Booth

      #3
      Re: re Insanity

      Tim Daneliuk wrote:
      [color=blue]
      >
      > I tried this:
      >
      > y=re.compile(r' \[PROMPT:.*\]')
      >
      > Which works fine when the text is exactly "[PROMPT:whatever]" but
      > does not match on:
      >
      > "something [PROMPT:foo] something [PROMPT:bar] something ..."
      >
      > The overall goal is to identify the beginning and end of each [PROMPT...]
      > string in the line.
      >[/color]

      The answer sort of depends on exactly what can be in your optional text:
      [color=blue][color=green][color=darkred]
      >>> import re
      >>> s = "something [PROMPT:foo] something [PROMPT:bar] something ..."
      >>> y=re.compile(r' \[PROMPT:.*\]')
      >>> y.findall(s)[/color][/color][/color]
      ['[PROMPT:foo] something [PROMPT:bar]'][color=blue][color=green][color=darkred]
      >>> y=re.compile(r' \[PROMPT:.*?\]')
      >>> y.findall(s)[/color][/color][/color]
      ['[PROMPT:foo]', '[PROMPT:bar]'][color=blue][color=green][color=darkred]
      >>> y=re.compile(r' \[PROMPT:[^]]*\]')
      >>> y.findall(s)[/color][/color][/color]
      ['[PROMPT:foo]', '[PROMPT:bar]'][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      ..* will match as long a string as possible.

      ..*? will match as short a string as possible. By default this won't match
      any newlines.

      [^]]* will match as long a string that doesn't contain ']' as possible.
      This will match newlines.

      Comment

      • Aahz

        #4
        Re: re Insanity

        In article <4ln9c2-0mh1.ln1@eskimo .tundraware.com >,
        Tim Daneliuk <tundra@tundraw are.com> wrote:[color=blue]
        >
        >Given an arbitrary string, I want to find each individual instance of
        >text in the form: "[PROMPT:optional text]"
        >
        >I tried this:
        >
        > y=re.compile(r' \[PROMPT:.*\]')
        >
        >Which works fine when the text is exactly "[PROMPT:whatever]" but
        >does not match on:
        >
        > "something [PROMPT:foo] something [PROMPT:bar] something ..."
        >
        >The overall goal is to identify the beginning and end of each [PROMPT...]
        >string in the line.
        >
        >Ideas anyone?[/color]

        Yeah, read the Friedl book. (Okay, so that's not gonna help right now,
        but trust me, if you're going to write lots of regexes, READ THAT BOOK.)
        --
        Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

        "19. A language that doesn't affect the way you think about programming,
        is not worth knowing." --Alan Perlis

        Comment

        • Tim Daneliuk

          #5
          Re: re Insanity

          Aahz wrote:[color=blue]
          > In article <4ln9c2-0mh1.ln1@eskimo .tundraware.com >,
          > Tim Daneliuk <tundra@tundraw are.com> wrote:
          >[color=green]
          >>Given an arbitrary string, I want to find each individual instance of
          >>text in the form: "[PROMPT:optional text]"
          >>
          >>I tried this:
          >>
          >> y=re.compile(r' \[PROMPT:.*\]')
          >>
          >>Which works fine when the text is exactly "[PROMPT:whatever]" but
          >>does not match on:
          >>
          >> "something [PROMPT:foo] something [PROMPT:bar] something ..."
          >>
          >>The overall goal is to identify the beginning and end of each [PROMPT...]
          >>string in the line.
          >>
          >>Ideas anyone?[/color]
          >
          >
          > Yeah, read the Friedl book. (Okay, so that's not gonna help right now,
          > but trust me, if you're going to write lots of regexes, READ THAT BOOK.)[/color]

          I've read significant parts of it. The problem is that I don't write
          re often enough to recall all the subtle details ... plus I am getting
          old and feeble... ;)

          --
          ----------------------------------------------------------------------------
          Tim Daneliuk tundra@tundrawa re.com
          PGP Key: http://www.tundraware.com/PGP/

          Comment

          Working...