Helder Lucas
Helder Lucas

Reputation: 3343

Regex troubleshooting with very specific pattern on newlines

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

Answers (3)

user13469682
user13469682

Reputation:

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

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

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

user13469682
user13469682

Reputation:

Try find (\r?\n)?(?:\r?\n){2,} replace $1\n
optional match line break write back greater than 2.

demo

or using \R (\R)?\R{2,}

demo2

Upvotes: 0

Related Questions