#1
  1. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351

    HTML Nested Tag Replacement


    Groove and I ran into an interesting question over on the PHP forum. There was a member who wanted to replace only CERTAIN span tags (with a particular class name) with <strong> tags, also replacing the corresponding closing span with a closing strong. When there are no nested tags at all, the application is a trivial regex. However, what about nested tags? What about a replaceable span with non-replaceable spans inside of it? What about nested replaceable spans?

    Since this forum is pretty dead, I thought I'd posit the question here as well. Check out the various PHP solutions we've come up with. My very last post in that thread solves the problem for everything but a replaceable span inside another replaceable span.

    What do you guys think? Is it possible in a regexp?

    -Dan

    Comments on this post

    • drgroove agrees : Does this post count as cross-posting? :D
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  2. #2
  3. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265
    I still plan on solving this damn thing.
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Good luck to you! I'm still waiting for requinix to drop a regex bomb on us.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265
    Originally Posted by ManiacDan
    Good luck to you! I'm still waiting for requinix to drop a regex bomb on us.

    -Dan
    Right? He'd be the one to do it, if it can be done. I know it's possible to write recursive regex (as well as look-forward and look-behind)... just don't have enough regex-fu to pull it off myself. Yet.
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Norcross, GA
    Posts
    8
    Rep Power
    0
    Hi, I'm new here, but I'll contribute my 2 cents.

    In the traditional definition of regex, no, it's not possible. Regular expressions are supposed to be reducible to a finite state automata. The moment you add a register (in this case, a variable to remember what tag you're trying to match), you've cheated.

    But that's all just boring textbook talk. Many regex engines let you "cheat". The .NET regex engine is my weapon of choice, and it has a built in construct called a "balancing group definition" that would accomplish exactly what you're trying to do: keep track of nested pairs (matching html tags).

    I did a quick google search and didn't see an equivalent in PHP. But maybe you're better versed in PHP than I and can run with this.

IMN logo majestic logo threadwatch logo seochat tools logo