Regular expression x remove \n\r to text

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

Regular expression x remove \n\r to text

Post by webstyler »

Hi, I must transform html template into txt template

but I have any problem to make this regular expression:

remove all \n{2,}

for remove more than 2 occurencies of \n or \n\r

?

tx
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Code: Select all

^.*$
I believe this expression would match any character on a line. But not line breaks.

You should download regex coach:

http://weitz.de/regex-coach/#install

That will allow you to test your expressions on your desktop.

This site is a great resource too:

http://regular-expressions.info
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

Post by webstyler »

neophyte wrote:

Code: Select all

^.*$
I believe this expression would match any character on a line. But not line breaks.

You should download regex coach:

http://weitz.de/regex-coach/#install

That will allow you to test your expressions on your desktop.

This site is a great resource too:

http://regular-expressions.info
Hi, thks for info :)

But, I need to replace more than 2 > \n\r in a single \n

tx
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Regular expression questions belong in the regex forum. :?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Would that be, turn all \r\n into just \n? \n\r isn't used anywhere as far as I know.

Code: Select all

preg_replace("~\r(?=\n)~", "\n", $text);
If that's not what you meant, you'll need to give some examples as I don't seem to understand what you meant ;)
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

d11wtq wrote:Would that be, turn all \r\n into just \n?

Code: Select all

preg_replace("~\r(?=\n)~", "\n", $text);
If I'm not mistaken your regex is turning \r\n into \n\n. Remember that lookarounds don't 'consume' text.

Code: Select all

// Try:
preg_replace('~\r(?=\n)~', '', $text);
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

Post by webstyler »

d11wtq wrote:Would that be, turn all \r\n into just \n? \n\r isn't used anywhere as far as I know.

Code: Select all

preg_replace("~\r(?=\n)~", "\n", $text);
If that's not what you meant, you'll need to give some examples as I don't seem to understand what you meant ;)
tx

we must replace all >2 occurencies of \r OR \n OR \r\n OR \n\r with a single \n

tx
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

preg_replace("~[\r\n]{2,}~", "\n", $text);
?
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

That regex would also turn a single occurrence of \r\n into \n.

Credits for the code below goes to the WordPress coders. These lines can be found in the auto_p() function:

Code: Select all

$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines 
	$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
Post Reply