You need a regular expression to match a letter followed by a number or capital

I need a regular expression that can replace a lowercase letter nwith a new line, but only when followed by a number 0-9or an uppercase letter.

For example, the line:
Company Buildingn100 Prospect Way

Must be converted to:
Company Building
100 Prospect Way

I am trying to misinform this data in PHP, so the resulting expression must be compatible.

+3
source share
2 answers

Try the following:

n(?=[\dA-Z])

In PHP ( working example ):

$str = preg_replace("/n(?=[\dA-Z])/", "\n", $str);

(?=...)- this is a positive look - it checks that after nwe match, but do not match it, so the next character is not replaced.

+4
source
$result = preg_replace("/n(?=[\dA-Z])/", "\n", $subject);

, ASCII.

$result = preg_replace("/n(?=[\d\p{Lu}])/u", "\n", $subject);

Unicode.

+3

Source: https://habr.com/ru/post/1786371/


All Articles