[英]Need a regular expression to match letter followed by number or capital
I need a regular expression that can replace the lowercase letter n
with a newline, but only when it is followed by a digit 0-9
or a capital letter. 我需要一个正则表达式,可以用换行符替换小写字母
n
,但只有当后面跟一个数字0-9
或大写字母时。
For example, the string: 例如,字符串:
Company Buildingn100 Prospect Way
Should convert into: 应该转换成:
Company Building
100 Prospect Way
I'm trying to sanitize this data in PHP, so the resulting expression needs to be compatible. 我正在尝试用PHP清理这些数据,因此生成的表达式需要兼容。
Try this: 试试这个:
n(?=[\dA-Z])
In PHP ( working example ): 在PHP( 工作示例 )中:
$str = preg_replace("/n(?=[\dA-Z])/", "\n", $str);
(?=...)
is a positive lookahead - it checks what's after the n
we matched, but doesn't match it, so the next character isn't replaced. (?=...)
是一个积极的先行 - 它检查我们匹配的n
后面的内容,但是不匹配,所以下一个字符不会被替换。
$result = preg_replace("/n(?=[\dA-Z])/", "\n", $subject);
will do this if by capital letter you mean ASCII letters. 如果用大写字母表示ASCII字母,我们会这样做。
$result = preg_replace("/n(?=[\d\p{Lu}])/u", "\n", $subject);
if you're using Unicode. 如果您使用的是Unicode。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.