SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast mitsy's Avatar
    Join Date
    Jul 2006
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    see your knowledge on "ereg" (help)

    i have a string="aaa bbb ccc ddd eee ddd fff"
    and i want to grab a sub string which starts with "bbb" and ends with the first "ddd" not the second or more if there any.

    so i want to grab this="bbb ccc ddd"

    but the problem is there are two "ddd"s in my string

    i used this regexp=
    eregi("aaa(.*)ddd",$string,$out);
    echo"$out[0]"; (that "[0]" includes your limitations of "aaa" and "ddd" which i want it to be)


    but this code sees a sub string which ends with the last "ddd" of the main string so my output is becomes like this=bbb ccc ddd eee ddd

    but i want "bbb ccc ddd"

    finally i want my substring to start with "bbb", and end with the first "ddd" coming after "bbb"

    pleaseeeeeeeeeee help me about this, this regexp will also help many people
    thanks...

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    eregi("aaa(.*)ddd",$string,$out);

    well, you want in that case:

    eregi("(aaa.+ddd)",$string,$out);

    from what you're saying. Also it's $out[1] for the first match, $out[0] is the entire matched string.

  3. #3
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don't use the posix functions (ereg, eregi, etc) so I don't know if they support ungreedy matching or not. There is really no good reason to use them from what I know of them, as the perl-compatible regex functions are faster and have more features

    here's how you would do that using preg_match() :
    PHP Code:
         preg_match("#aaa(.*?)ddd#i",$string,$out); 
    the ? after the .* makes the matching "ungreedy", in that regex's normally try to match the largest string possible, which is why your pattern doesn't stop after it hits the first ddd, and this tells the regex engine to stop after it hits the first case of ddd.

  4. #4
    SitePoint Enthusiast mitsy's Avatar
    Join Date
    Jul 2006
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i tried preg_match at first but that command does not grab a substring which contains linebreaks.

    and i couldnt make it grab. if there is way for preg_match i really prefer that!

  5. #5
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you want the dot (.) to match line breaks, just add the 's' modifier:
    PHP Code:
       preg_match("#aaa(.*?)ddd#is",$string,$out); 
    there is a really handy cheatsheet that covers all of the modifiers, meta characters, etc. here


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
  •