php - Regex troubleshooting with very specific pattern on newlines
Get the solution ↓↓↓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 :)
Answer
Solution:
Different way.
Try(\R?\R)\R+
replace$1
demo
Features:
- Single, un-quantified capture to be written back
- No alternation
- Uses complete captured line break(s) for write back
- Most efficient way for what it does, 114 steps on user sample
Answer
Solution:
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).
Answer
Solution:
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,}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your lock file does not contain a compatible set of packages. please run composer update.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.