Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

tags in tags [bla bla[bla]bla bla]

Last post 04-11-2008, 10:16 AM by ddrudik. 6 replies.
Sort Posts: Previous Next
  •  04-09-2008, 6:03 PM 41247

    tags in tags [bla bla[bla]bla bla]

    hi all,

    i need some regex to get over this situation.

    i couldnt figure it out myself so if you can help me, that would be great.

    my problem is that i need to extract some tags. for example;

    bla bla bla {{ tag starts here {{ internal tag }} and it continues.. }} like this..

    I need to extract :

    {{ tag starts here {{ internal tag }} and it continues.. }}

    portions of the text but i get results like this:

    {{ tag starts here {{ internal tag }}

    when the regex sees the }} it ends up there.

    Does it have a solution in regex? 


    The brightest flame burns quickest..
    Filed under: , ,
  •  04-09-2008, 8:11 PM 41250 in reply to 41247

    Re: tags in tags [bla bla[bla]bla bla]

    Traditionally the simple answer to your question is 'no' because regex's cannot count (in this case, count the number of {{'s and match until you ge to the same number of }}'s ).

    However, there are some extensions that can help you here but only in some languages and regex engines. Specifically, the .NET based regex engine can do this by having  what are called balanced groups, and PCRE can do this by defining a recursion on the appropriate part of the pattern. Perl allows you to execute your own custom code in the middle of the pattern matching process and so you can add the necessary code for yourself. Google will find several generic examples of each of these techniques.

    However, without knowing more about your specific situation we can't help you much further. Please see the posting guildelines (the 'sticky' note at the top of this forum) as to what you need to tell us.

    Susan

     

  •  04-10-2008, 3:14 AM 41259 in reply to 41250

    Re: tags in tags [bla bla[bla]bla bla]

    I'm using PHP and PCRE type of RegEx (preg_match_all) and i'm trying to parse the wikipedia content which i got from XML and Wiki build its content by adding variables into {{ }} tags and sometimes there are {{ }} tags again in the variable listing. So you say that single regex wont match my needs and i need to write some PHP code? Well, maybe thats the safest way..?

    The brightest flame burns quickest..
  •  04-10-2008, 10:06 AM 41267 in reply to 41259

    Re: tags in tags [bla bla[bla]bla bla]

    bla bla bla {{ tag starts here {{ internal tag }} and it continues.. }} like this..

    Adapted from Friedl's book, p.478:

    \{{2}(?:[^{}]++|(?R))*\}{2}


  •  04-10-2008, 6:08 PM 41271 in reply to 41267

    Re: tags in tags [bla bla[bla]bla bla]

    WOW! I think this will work only a tag is in a tag, if there's a third tag, this will not work.. But I think this will work for all the situations in my script. Thank you very very much! And Susan thank you too for your interest.

    The brightest flame burns quickest..
  •  04-10-2008, 7:12 PM 41272 in reply to 41271

    Re: tags in tags [bla bla[bla]bla bla]

    ddrudik's answer will work for nested tags of any depth. It is only limited by the memory available for the PCRE stack.

    Susan 

  •  04-11-2008, 10:16 AM 41286 in reply to 41271

    Re: tags in tags [bla bla[bla]bla bla]

    tpaksu:
    WOW! I think this will work only a tag is in a tag, if there's a third tag, this will not work.. But I think this will work for all the situations in my script. Thank you very very much! And Susan thank you too for your interest.

    bla bla bla {{ tag starts here and it continues.. }} like this..
    bla bla bla {{ tag starts here {{ internal tag }} and it continues.. }} like this..
    bla bla bla {{ tag starts here {{ internal tag }} and it continues.. tag starts here {{ internal tag }}  }} like this..
    bla bla bla {{ tag starts here {{ internal tag }} and it continues.. tag starts here {{ tag starts here {{ internal tag }} internal tag }} }} like this..

View as RSS news feed in XML