[PHP] ereg_replace - How do I stop it being greedy?
Hi, I've got a problem with regular expression syntax with ereg_replace: ereg_replace(':start:(.*):end:','this is \\1',':start: first :end: middle :start: last :end:'); returns - this is first :end: middle :start: last but I want - this is first middle this is last The problem seems to be that ereg_replace is being greedy on the match and matching as much as it can instead of just finding the first match, handling that and then going on to the next match. I can get it to work with preg_replace i.e. preg_replace(':start:(.*?):end:','this \\1',':start first :end: middle :start: last :end:') returns - this is first middle this is last But my actual string is on multiple lines, and preg_replace doesn't seem to continue trying to match on the next line, whereas ereg_replace happily treats newlines just like any other character. So how do I stop ereg_replace being greedy or alternatively get preg_replace to treat multiple lines as a single source string? George -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] ereg_replace - How do I stop it being greedy?
look into the s modifier...it makes a dot match a newline as well, where normally it wouldn't jack -Original Message- From: George Whiffen [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 1:09 PM To: [EMAIL PROTECTED] Subject: [PHP] ereg_replace - How do I stop it being greedy? Hi, I've got a problem with regular expression syntax with ereg_replace: ereg_replace(':start:(.*):end:','this is \\1',':start: first :end: middle :start: last :end:'); returns - this is first :end: middle :start: last but I want - this is first middle this is last The problem seems to be that ereg_replace is being greedy on the match and matching as much as it can instead of just finding the first match, handling that and then going on to the next match. I can get it to work with preg_replace i.e. preg_replace(':start:(.*?):end:','this \\1',':start first :end: middle :start: last :end:') returns - this is first middle this is last But my actual string is on multiple lines, and preg_replace doesn't seem to continue trying to match on the next line, whereas ereg_replace happily treats newlines just like any other character. So how do I stop ereg_replace being greedy or alternatively get preg_replace to treat multiple lines as a single source string? George -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] ereg_replace - How do I stop it being greedy?
Thanks Jack, preg_replace with an s modifier works a treat. I'm still curious as to how to get ereg_replace to work as well. Everything I read about regex/Posix Regular Expressions, seems to suggest that a ? should also work with ereg_replace! George Jack Dempsey wrote: look into the s modifier...it makes a dot match a newline as well, where normally it wouldn't jack -Original Message- From: George Whiffen [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 1:09 PM To: [EMAIL PROTECTED] Subject: [PHP] ereg_replace - How do I stop it being greedy? Hi, I've got a problem with regular expression syntax with ereg_replace: ereg_replace(':start:(.*):end:','this is \\1',':start: first :end: middle :start: last :end:'); returns - this is first :end: middle :start: last but I want - this is first middle this is last The problem seems to be that ereg_replace is being greedy on the match and matching as much as it can instead of just finding the first match, handling that and then going on to the next match. I can get it to work with preg_replace i.e. preg_replace(':start:(.*?):end:','this \\1',':start first :end: middle :start: last :end:') returns - this is first middle this is last But my actual string is on multiple lines, and preg_replace doesn't seem to continue trying to match on the next line, whereas ereg_replace happily treats newlines just like any other character. So how do I stop ereg_replace being greedy or alternatively get preg_replace to treat multiple lines as a single source string? George -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]