#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    178
    Rep Power
    204

    Very complex regex


    hi guys

    i have following type of strings
    Code:
    {tag}this string can have anything in it, including html tags and newlines etc, even it can have curly brackets like {variable}.. etc{jag}whatever,whatever,whatever{/jag}{/tag}

    where jag-tags always come before the closing {/tag} I have to fetch values between {tag}*{/tag} pair for which I am using the following regex

    Code:
    preg_match_all("/\{tag\}(.*?)(\{jag\}(.*?)\{jag\})?\{\/tag\}/",$str,$matches);

    it gives me my target values in $matches[1];

    But now the problem is that in some cases {tag} is not closed properly e.g please consider the following,


    Code:
    {tag}this string can have anything in it, including html tags and newlines etc, even it can have curly brackets like {variable}.. etc{jag}whatever,whatever,whatever{/jag}{tag}
    <html><html>lots of html</html>
    {tag}blah blah blah{/tag}

    now this regex will fetch from the first starting tag till it finds the last next closing tag and hence creating problems.

    Now I want to write such a regex that sees if an opening tag is found without closing the previous one then it stops reading further characters, and create an array of such values separately. So that I can find out which strings have incorrectly closed tags e.g in above string i have UNDERLINED the two types of strings that I want to store ideally in separate index of final array so that I know which are the values that have incorrectly closing tags


    thank you very much for your help.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Generally, parsing markup cannot easily be done with regexp. See this thread for a good example. Your best bet is to tackle this procedurally.

    -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.

IMN logo majestic logo threadwatch logo seochat tools logo