FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » How do I do this search and replace?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: How do I do this search and replace? [message #172350 is a reply to message #172346] Mon, 14 February 2011 09:54 Go to previous messageGo to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma:
Senior Member
Curtis Dyer wrote:
> The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
>
>> BootNic wrote:
>>> On Sat, 12 Feb 2011 13:15:44 -0800 (PST)
>>> "laredotornado(at)zipmail(dot)com" <laredotornado(at)zipmail(dot)com> wrote:
>>>
>>>> I'm using PHP 5. I want to search for all instances of the
>>>> "\n" character in my $contents variable and replace it with
>>>> "\r\n". However, I do not want to replace the "\n" if the
>>>> character immediately before is already a "\r". How would I
>>>> construct such a search and replace to do this?
>>> [url][/color]
[color=royalblue]>>> http://www.regular-expressions.info/lookaround.html#lookbehind[/color]
[color=royalblue]>>> [/url] [/color]
>>>
>>> using negative lookbehind
>>>
>>> $contents = preg_replace("/(?<!\r)\n/", "\r\n", $contents);
>
> And if you want to account for CRs as EOLs, you might try:
>
> // Don't replace when CRs precede LFs nor when LFs follow CRs
> $contents = preg_replace(
> '/ (?<!\r) \n | \r (?!\n) /x',
> "\r\n",
> $contents
> );
>
>> yeah I can intuitively see exactly what THAT does.
>
> This really isn't all that difficult.
>
> Like all tools, regexes have their uses where appropriate. To
> outright dismiss them is unwise, in my opinion.
>

I think they are fine one time tools, where you are e.g. hacking a file
into a format and do it but once.

I think they are ghastly for production code, as they are in essence a
programming language in their own right, and one that is so opaque to
any sort of formal structuring, that I cant say they represent good
practice.



> With regard to the OP's case, I don't claim regexes are superior
> to all alternatives, but they are certainly viable and get the job
> done.
>


> <snip>
>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Curious problem
Next Topic: Hot Sameera Hot Cleavage show - Download
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 10:50:38 GMT 2024

Total time taken to generate the page: 0.03999 seconds