Stupid Regex Question

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

    Stupid Regex Question

    I'm feeling dumb:

    str = "<tag>text<inne rtag>moretext</innertag></tag><tag>text</tag>"

    How can I get a list like ["text<innertag> moretext</innertag>",
    "text"] using regexes? I'm starting to believe it's not possible.

    Yeah, I know about the terror stories about using regexes to parse
    HTML, but in this particular case, using a SAX parser would be an
    horrendous overhead (I mean, it's a ridiculously simple string).



    =-
    Jonas Galvez
    jonasgalvez.com/blog
    macromedia.com/go/team





  • Noah from IT Goes Click

    #2
    Re: Stupid Regex Question

    small loop problem?

    Jonas Galvez wrote:
    [color=blue]
    > I'm feeling dumb:
    >
    > str = "<tag>text<inne rtag>moretext</innertag></tag><tag>text</tag>"
    >
    > How can I get a list like ["text<innertag> moretext</innertag>",
    > "text"] using regexes? I'm starting to believe it's not possible.
    >
    > Yeah, I know about the terror stories about using regexes to parse
    > HTML, but in this particular case, using a SAX parser would be an
    > horrendous overhead (I mean, it's a ridiculously simple string).
    >
    >
    >
    > =-
    > Jonas Galvez
    > jonasgalvez.com/blog
    > macromedia.com/go/team
    >
    >
    >
    >
    >[/color]

    Comment

    • ZeN0

      #3
      Re: Stupid Regex Question

      Jonas Galvez:
      [color=blue]
      > str = "<tag>text<inne rtag>moretext</innertag></tag><tag>text</tag>"
      > How can I get a list like ["text<innertag> moretext</innertag>", "text"]
      > using regexes? I'm starting to believe it's not possible.[/color]

      You can try with:

      re.findall("<ta g>(.*?)</tag>",str)

      (if you don't have nested tag like in this case)
      ..*? means anychar (.) many times or never (*) but least as possible (?)

      Greetings,
      Zenzero

      Comment

      Working...