Reputation: 3343
I am trying to manipulate a string in PHP to accomplish the following:
Example Input:
"""
line1\n
\n
line2\n
\n
\n
\n
line3\n
\n
\n
\n
\n
line4\n
\n
line5
"""
Desired Output:
"""
line1\n
line2\n
\n
line3\n
\n
line4\n
line5
"""
So far to achieve this in my javascript I have: text.split('\n\n').join('\n')
and on the the PHP side I'm using this expression: preg_replace('~(\R{2})\R+~', '$1', $text)
.
Those two methods do the job, but this process seems very funky and since regex is not my thing at all I would like to know if there is a better way to do this? Im sure there is :)
Upvotes: 0
Views: 69
Reputation:
Different way.
Try (\R?\R)\R+
replace $1
demo
Features:
Upvotes: 1
Reputation: 626932
You may use
preg_replace('~(\R{2})\R+|(\R){2}~', '$1$2', $text)
See the regex demo
Details
(\R{2})\R+
- two line break sequences captured in Group 1 and then one or more line break sequences|
- or(\R){2}
- two line break sequences capturing the last one of them into Group 2.The replacement is either the value of Group 1 or Group 2 (since one of them is always an empty string).
Upvotes: 1
Reputation:
Try find (\r?\n)?(?:\r?\n){2,}
replace $1\n
optional match line break write back greater than 2.
or using \R (\R)?\R{2,}
Upvotes: 0