RE: [PHP] RegExp question: how to add a number?
-Original Message- From: Andre Polykanine [mailto:an...@oire.org] Sent: 14 October 2010 21:42 Hi everyone, I hope you're doing well (haven't written here for a long time :-)). The question is as follows: I have a regexp that would do the following. If the string begins with Re:, it will change the beginning to Re[2]:; if it doesn't, then it would add Re: at the beginning. But (attention, here it is!) if the string starts with something like Re[4]:, it should replace it by Re[5]:. Here's the code: $start=mb_strtolower(mb_substr($f['Subject'], 0, 3)); if ($start==re:) { $subject=preg_replace(/^re:(.+?)$/usi, re[2]:$1, $f['Subject']); } elseif ($start==re[) { // Here $1+1 doesn't work, it returns Re[4+1]:! $subject=preg_replace(/^re\[(\d+)\]:(.+?)$/usi, re[$1+1]:$2, $f['Subject']); } else { $subject=Re: .$f['Subject']; } I know there actually exists a way to do the numeral addition (Re[5]:, not Re[4+1]:). How do I manage to do this? This looks like a job for the e modifier (see http://php.net/manual/en/reference.pcre.pattern.modifiers.php, and example #4 at http://php.net/preg_replace). Something like: $subject = preg_replace(/^re\[(\d+)\:](.+?)$/eusi, 're['.(\\1+1).']:\\2', $f['Subject']); Cheers! Mike -- Mike Ford, Electronic Information Developer, Libraries and Learning Innovation, Leeds Metropolitan University, C507 City Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RegExp question: how to add a number?
On 15 October 2010 10:16, Ford, Mike m.f...@leedsmet.ac.uk wrote: -Original Message- From: Andre Polykanine [mailto:an...@oire.org] Sent: 14 October 2010 21:42 Hi everyone, I hope you're doing well (haven't written here for a long time :-)). The question is as follows: I have a regexp that would do the following. If the string begins with Re:, it will change the beginning to Re[2]:; if it doesn't, then it would add Re: at the beginning. But (attention, here it is!) if the string starts with something like Re[4]:, it should replace it by Re[5]:. Here's the code: $start=mb_strtolower(mb_substr($f['Subject'], 0, 3)); if ($start==re:) { $subject=preg_replace(/^re:(.+?)$/usi, re[2]:$1, $f['Subject']); } elseif ($start==re[) { // Here $1+1 doesn't work, it returns Re[4+1]:! $subject=preg_replace(/^re\[(\d+)\]:(.+?)$/usi, re[$1+1]:$2, $f['Subject']); } else { $subject=Re: .$f['Subject']; } I know there actually exists a way to do the numeral addition (Re[5]:, not Re[4+1]:). How do I manage to do this? This looks like a job for the e modifier (see http://php.net/manual/en/reference.pcre.pattern.modifiers.php, and example #4 at http://php.net/preg_replace). Something like: $subject = preg_replace(/^re\[(\d+)\:](.+?)$/eusi, 're['.(\\1+1).']:\\2', $f['Subject']); Cheers! Mike Watch out for the missing '[1]'. This needs to become '[2]' and not '[1]'. The callback seems to be the only way I could get the regex to work. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RegExp question: how to add a number?
On Fri, Oct 15, 2010 at 5:52 AM, Richard Quadling rquadl...@gmail.com wrote: On 15 October 2010 10:16, Ford, Mike m.f...@leedsmet.ac.uk wrote: -Original Message- From: Andre Polykanine [mailto:an...@oire.org] Sent: 14 October 2010 21:42 Hi everyone, I hope you're doing well (haven't written here for a long time :-)). The question is as follows: I have a regexp that would do the following. If the string begins with Re:, it will change the beginning to Re[2]:; if it doesn't, then it would add Re: at the beginning. But (attention, here it is!) if the string starts with something like Re[4]:, it should replace it by Re[5]:. Here's the code: $start=mb_strtolower(mb_substr($f['Subject'], 0, 3)); if ($start==re:) { $subject=preg_replace(/^re:(.+?)$/usi, re[2]:$1, $f['Subject']); } elseif ($start==re[) { // Here $1+1 doesn't work, it returns Re[4+1]:! $subject=preg_replace(/^re\[(\d+)\]:(.+?)$/usi, re[$1+1]:$2, $f['Subject']); } else { $subject=Re: .$f['Subject']; } I know there actually exists a way to do the numeral addition (Re[5]:, not Re[4+1]:). How do I manage to do this? This looks like a job for the e modifier (see http://php.net/manual/en/reference.pcre.pattern.modifiers.php, and example #4 at http://php.net/preg_replace). Something like: $subject = preg_replace(/^re\[(\d+)\:](.+?)$/eusi, 're['.(\\1+1).']:\\2', $f['Subject']); Cheers! Mike Watch out for the missing '[1]'. This needs to become '[2]' and not '[1]'. The callback seems to be the only way I could get the regex to work. How about preg_replace_callback()? Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RegExp question: how to add a number?
On 15 October 2010 15:45, Andrew Ballard aball...@gmail.com wrote: On Fri, Oct 15, 2010 at 5:52 AM, Richard Quadling rquadl...@gmail.com wrote: On 15 October 2010 10:16, Ford, Mike m.f...@leedsmet.ac.uk wrote: -Original Message- From: Andre Polykanine [mailto:an...@oire.org] Sent: 14 October 2010 21:42 Hi everyone, I hope you're doing well (haven't written here for a long time :-)). The question is as follows: I have a regexp that would do the following. If the string begins with Re:, it will change the beginning to Re[2]:; if it doesn't, then it would add Re: at the beginning. But (attention, here it is!) if the string starts with something like Re[4]:, it should replace it by Re[5]:. Here's the code: $start=mb_strtolower(mb_substr($f['Subject'], 0, 3)); if ($start==re:) { $subject=preg_replace(/^re:(.+?)$/usi, re[2]:$1, $f['Subject']); } elseif ($start==re[) { // Here $1+1 doesn't work, it returns Re[4+1]:! $subject=preg_replace(/^re\[(\d+)\]:(.+?)$/usi, re[$1+1]:$2, $f['Subject']); } else { $subject=Re: .$f['Subject']; } I know there actually exists a way to do the numeral addition (Re[5]:, not Re[4+1]:). How do I manage to do this? This looks like a job for the e modifier (see http://php.net/manual/en/reference.pcre.pattern.modifiers.php, and example #4 at http://php.net/preg_replace). Something like: $subject = preg_replace(/^re\[(\d+)\:](.+?)$/eusi, 're['.(\\1+1).']:\\2', $f['Subject']); Cheers! Mike Watch out for the missing '[1]'. This needs to become '[2]' and not '[1]'. The callback seems to be the only way I could get the regex to work. How about preg_replace_callback()? Andrew Already provided an example using that : http://news.php.net/php.general/308728 It was the 'e' modifier I couldn't get to work, though I suppose, as the code is eval'd, I should have been able to do it. The callback just seems a LOT easier. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RegExp question: how to add a number?
On Fri, Oct 15, 2010 at 11:07 AM, Richard Quadling rquadl...@gmail.com wrote: On 15 October 2010 15:45, Andrew Ballard aball...@gmail.com wrote: On Fri, Oct 15, 2010 at 5:52 AM, Richard Quadling rquadl...@gmail.com wrote: On 15 October 2010 10:16, Ford, Mike m.f...@leedsmet.ac.uk wrote: -Original Message- From: Andre Polykanine [mailto:an...@oire.org] Sent: 14 October 2010 21:42 Hi everyone, I hope you're doing well (haven't written here for a long time :-)). The question is as follows: I have a regexp that would do the following. If the string begins with Re:, it will change the beginning to Re[2]:; if it doesn't, then it would add Re: at the beginning. But (attention, here it is!) if the string starts with something like Re[4]:, it should replace it by Re[5]:. Here's the code: $start=mb_strtolower(mb_substr($f['Subject'], 0, 3)); if ($start==re:) { $subject=preg_replace(/^re:(.+?)$/usi, re[2]:$1, $f['Subject']); } elseif ($start==re[) { // Here $1+1 doesn't work, it returns Re[4+1]:! $subject=preg_replace(/^re\[(\d+)\]:(.+?)$/usi, re[$1+1]:$2, $f['Subject']); } else { $subject=Re: .$f['Subject']; } I know there actually exists a way to do the numeral addition (Re[5]:, not Re[4+1]:). How do I manage to do this? This looks like a job for the e modifier (see http://php.net/manual/en/reference.pcre.pattern.modifiers.php, and example #4 at http://php.net/preg_replace). Something like: $subject = preg_replace(/^re\[(\d+)\:](.+?)$/eusi, 're['.(\\1+1).']:\\2', $f['Subject']); Cheers! Mike Watch out for the missing '[1]'. This needs to become '[2]' and not '[1]'. The callback seems to be the only way I could get the regex to work. How about preg_replace_callback()? Andrew Already provided an example using that : http://news.php.net/php.general/308728 It was the 'e' modifier I couldn't get to work, though I suppose, as the code is eval'd, I should have been able to do it. The callback just seems a LOT easier. Sorry - I missed the callback function in there. The loop threw me off because I thought that was part of the solution rather than a test container. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RegExp question: how to add a number?
On Thu, Oct 14, 2010 at 1:42 PM, Andre Polykanine an...@oire.org wrote: But (attention, here it is!) if the string starts with something like Re[4]:, it should replace it by Re[5]:. Regular expressions do not support any mathematical operations. Instead, you need to use preg_match() to extract the number inside the brackets, increment it, and build a new string using simple concatenation (.). elseif ($start==re[) { if (preg_match('/^re\[(\d+)\](.*)/i', $f['Subject'], $matches) 0) { $f['Subject'] = 'Re[' . ($matches[1] + 1) . ']' . $matches[2]; } else { // no closing brace -- now what? } } David
Re: [PHP] RegExp question: how to add a number?
On 14 October 2010 21:42, Andre Polykanine an...@oire.org wrote: Hi everyone, I hope you're doing well (haven't written here for a long time :-)). The question is as follows: I have a regexp that would do the following. If the string begins with Re:, it will change the beginning to Re[2]:; if it doesn't, then it would add Re: at the beginning. But (attention, here it is!) if the string starts with something like Re[4]:, it should replace it by Re[5]:. Here's the code: $start=mb_strtolower(mb_substr($f['Subject'], 0, 3)); if ($start==re:) { $subject=preg_replace(/^re:(.+?)$/usi, re[2]:$1, $f['Subject']); } elseif ($start==re[) { // Here $1+1 doesn't work, it returns Re[4+1]:! $subject=preg_replace(/^re\[(\d+)\]:(.+?)$/usi, re[$1+1]:$2, $f['Subject']); } else { $subject=Re: .$f['Subject']; } I know there actually exists a way to do the numeral addition (Re[5]:, not Re[4+1]:). How do I manage to do this? Thanks! Can you adapt this ... ?php $s_Text = 'Re: A dummy subject.'; foreach(range(1,10) as $i_Test) { echo $s_Text = preg_replace_callback ( '`^Re(\[(\d++)\])?:`', function($a_Match) { if (count($a_Match) == 1) { $i_Count = 2; } else { $i_Count = 1 + $a_Match[2]; } return Re[$i_Count]:; }, $s_Text ), PHP_EOL; } Outputs ... Re[2]: A dummy subject. Re[3]: A dummy subject. Re[4]: A dummy subject. Re[5]: A dummy subject. Re[6]: A dummy subject. Re[7]: A dummy subject. Re[8]: A dummy subject. Re[9]: A dummy subject. Re[10]: A dummy subject. Re[11]: A dummy subject. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php