Page 3 of 3 FirstFirst 123
Results 21 to 23 of 23

Thread: Regex extract sequence of a certain format from string

  1. #21
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You could do it by putting preg_match() in a loop and incrementing the offset each time, for the length of the string. Not very efficient, I guess. You'd have to keep track of the match positions, as well, or you'd count some of the matches more than once.
    See above

    It's not that inefficient considering that string operations aren't too bad and regex is bad, but the difference between an internally recursive function and looping it is minimal.

    It works. Now I understand "end" in the correct way... I had missed that completely before reading through the documentation trying to figure it out. Thanks.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #22
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    PCRE and perl have built in recursive abilities see http://php.net/manual/en/regexp.reference.recursive.php. Finding information on recursion or examples of it has been rather difficult to track down, but I was able to get this one from the kind people at regexadvice.com

    Code:
    <?php
    $text = '[one]text [two]text [three]text[/thre] text[/two] text[/one]';
    $regexp = '{((\[([^\]]+)\])((?:(?:(?!\[/?\3\]).)*+|(?1))*)(\[/\3\]))}si';
    while(preg_match($regexp,$text,$match)){
        $text = preg_replace($regexp,'<$3>$4</$3>',$text);
    }
    echo $text;
    ?>
    The above is something that will convert certain bbcode to html code. In this case
    Code:
    [one]text [two]text [three]text[/thre] text[/two] text[/one]
    is converted to
    Code:
    <one>text <two>text [three]text[/thre] text</two> text</one>
    notice that the mismatched bbcode was ignored.

    It was also around this time that regex was making my head hurt so I stopped reading up on it. As I recall, this PCRE example still needs a touch of work, but for me should be a perfect example for experimenting with recursion later on when the desire/need arises to investigate this aspect of PCRE. I do not recall if/what/where this pattern failed, but yeah, it is beyond my understanding of PCRE.

    On a side note, the script above looks like it should make sense, I have an understanding of all the terms, but with all the nested loops and different types going on in the PCRE given above it just makes my head hurt. It is like the feeling you get when you look at a card table covered with the 5000 pieces to a jigsaw puzzle! Sometimes it's best just to shove the pieces back into the box and put it away for a year or three.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #23
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    See above
    $#!+ ...

    I was tired last night.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •