【问题标题】:Keep a character found by preg_replace保留 preg_replace 找到的字符
【发布时间】:2017-07-07 23:35:16
【问题描述】:

我想替换一个点,后跟一个空格,后跟一个大写,我尝试使用这种模式将它替换为   

preg_replace('/\. [A-Z]/', '.    'With marriage came a move to the beautiful Birmingham, Alabama area. Diving in head first.');

它正在工作,但我首先失去了 Diving 的 D。

我怎样才能保留它?

【问题讨论】:

  • '/\.\s+(?=[A-Z])/'
  • preg_replace('/\. ([A-Z])/', '.    $1',$s ); $s 是你的字符串

标签: php regex uppercase


【解决方案1】:

将字母匹配模式放入非消耗前瞻:

'/\.\s+(?=[A-Z])/'

\s+ 将匹配 1 个或多个空格(或者如果您不想要,保留常规空格),(?=[A-Z]) 使引擎要求在字符串中的当前位置之后出现一个大写的 ASCII 字母。

查看PHP demo 打印With marriage came a move to the beautiful Birmingham, Alabama area.   Diving in head first.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2013-04-15
    相关资源
    最近更新 更多