dqd3690 2015-07-01 21:59
浏览 27

如何使用正则表达式获取字符串的一部分

I have this situation where I can have strings like this:

"Project\\V1\\Rest\\Car\\Controller"
"Project\\V1\\Rest\\Boat\\Controller"
"Project\\Action\\Truck"
"Project\\V1\\Rest\\Helicopter\\Controller"
"Parental\\Boat\\Action"

Just in case the string follow the pattern:

"Project\\V1\\Rest\\THE_DESIRED_WORD\\Controller"

I want to get THE_DESIRED_WORD.

That's why I'm thinking in a regular expression.

  • 写回答

3条回答 默认 最新

  • ds19891231 2015-07-01 22:05
    关注

    to use a regular expression, you need to escape the slash twice : once for the PHP string and once for the regex

    try this :

    $tab = array(
        "Project\\V1\\Rest\\Car\\Controller",
        "Project\\V1\\Rest\\Boat\\Controller",
        "Project\\V1\\Rest\\Helicopter\\Controller",
        "Project\\V1\\Rest\\Water\\Controller",
    );
    
    foreach ($tab as $s) {
        preg_match("!\\\\([^\\\\]*)\\\\Controller!U", $s, $result);
        var_dump($result);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测