dsa1234569 2014-07-27 18:58
浏览 334
已采纳

正则表达式 - 选取以大写字母开头的连续单词

I have to pick up consecutive capital-letter starting words in a text (using PHP preg_match()).

So in this text - "this is Some text" it should pick up word "Some", but in this text - "this is Another Piece Of text" it should pick up "Another Piece Of".

I currently have this expression - ([A-Z][a-z]+)+, but it only picks up every single capital case word. I need them in as a whole line (e.g - [0] => "Another Piece Of", but I currently get [0] => "Another", [1] => "Piece", [2] => "Of")

How should I update it so that it does what I need?

  • 写回答

1条回答 默认 最新

  • douangoo48863 2014-07-27 19:05
    关注

    You can use this:

    if (preg_match('~[A-Z][a-z]*(?> [A-Z][a-z]*)*~', $text, $m)) {
        echo $m[0];
    }
    

    (?> [A-Z][a-z]*)* represents optional other words.

    To be more flexible you can change it to (?>\s+[A-Z][a-z]*)*

    Note: if you need to deal with accented words, you can use the \p{Ll} and \p{Lu} character classes:

    if (preg_match('~\p{Lu}\p{Ll}*(?>\s+\p{Lu}\p{Ll}*)*~', $text, $m)) {
        echo $m[0];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器