Regular Expressions?

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

    Regular Expressions?

    I want to use a regular expression like the following:

    "*[\s|\w]*" + x +"[\s|\w]*\" + y + "*[\s|\w]*\" + y + "*[\s|\w]*"

    where x and y and varaiblae names and their value changes everytime.
    For some reason this gives me an Invalid Token error. Can I use
    variables in a regular Expression?

    thanks a lot


  • Diez B. Roggisch

    #2
    Re: Regular Expressions?

    > I want to use a regular expression like the following:[color=blue]
    >
    > "*[\s|\w]*" + x +"[\s|\w]*\" + y + "*[\s|\w]*\" + y + "*[\s|\w]*"
    >
    > where x and y and varaiblae names and their value changes everytime.
    > For some reason this gives me an Invalid Token error. Can I use
    > variables in a regular Expression?[/color]

    Sure. But what you wrote above is most probably syntactically wrong:

    "*[\s|\w]*" + x +"[\s|\w]*\" + y + "*[\s|\w]*\" + y + "*[\s|\w]*"
    ^^
    You escape the d'qoutes, so that not the variable is inserted but instead
    the text " + y + ". Then a [ starts, which denotes the beginning of a list
    - a thing not supported right here. The same problem arises before the
    second y.

    This works for me:

    "*[\s|\w]*" + x +"[\s|\w]*" + y + "*[\s|\w]*" + y + "*[\s|\w]*"

    However, I suggest you use the %-operator in such cases:

    "*[\s|\w]*%s[\s|\w]*%s*[\s|\w]*%s*[\s|\w]*" % (x, y, y)


    --
    Regards,

    Diez B. Roggisch

    Comment

    • David M. Cooke

      #3
      Re: Regular Expressions?

      At some point, "Diez B. Roggisch" <deets_noospaam @web.de> wrote:
      [color=blue][color=green]
      >> I want to use a regular expression like the following:
      >>
      >> "*[\s|\w]*" + x +"[\s|\w]*\" + y + "*[\s|\w]*\" + y + "*[\s|\w]*"
      >>
      >> where x and y and varaiblae names and their value changes everytime.
      >> For some reason this gives me an Invalid Token error. Can I use
      >> variables in a regular Expression?[/color]
      >
      > Sure. But what you wrote above is most probably syntactically wrong:
      >
      > "*[\s|\w]*" + x +"[\s|\w]*\" + y + "*[\s|\w]*\" + y + "*[\s|\w]*"
      > ^^
      > You escape the d'qoutes, so that not the variable is inserted but instead
      > the text " + y + ". Then a [ starts, which denotes the beginning of a list
      > - a thing not supported right here. The same problem arises before the
      > second y.
      >
      > This works for me:
      >
      > "*[\s|\w]*" + x +"[\s|\w]*" + y + "*[\s|\w]*" + y + "*[\s|\w]*"
      >
      > However, I suggest you use the %-operator in such cases:
      >
      > "*[\s|\w]*%s[\s|\w]*%s*[\s|\w]*%s*[\s|\w]*" % (x, y, y)[/color]

      I'd suggest using re.escape() to make sure there's no nasty surprises
      inside of x and y:

      "*[\s|\w]*%s[\s|\w]*%s*[\s|\w]*%s*[\s|\w]*" % (re.escape(x),
      re.escape(y), re.escape(y))

      --
      |>|\/|<
      /--------------------------------------------------------------------------\
      |David M. Cooke
      |cookedm(at)phy sics(dot)mcmast er(dot)ca

      Comment

      Working...