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 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测