dongxun1978 2018-01-17 20:27
浏览 68
已采纳

正则表达式字符串里面有两个没有空格的特殊字符

I am trying to write a RegEx for preg_match_all in php to match a string inside 2 $ symbols, like $abc$ but only if it doesn't have a space, for example, I don't need to match $ab c$.

I wrote this regex /[\$]\S(.*)[\$]/U and some variations but can't get it to work.

Thanks for your help guys.

  • 写回答

2条回答 默认 最新

  • dongpaozhi5734 2018-01-17 20:34
    关注

    Overview

    Your regex: [\$]\S(.*)[\$]

    • [\$] - No point in escaping $ inside [] because it's already interpreted as the literal character. No point putting \$ inside [] because \$ is the escaped version. Just use one or the other [$] or \$.
    • \S(.*) Matches any non-whitespace character (once), followed by any character (except ) any number of times

    Code

    See regex in use here

    \$\S+\$
    
    • \$ Match $ literally
    • \S+ Match any non-whitespace character one or more times
    • \$ Match $ literally

    Usage

    $re = '/\$\S+\$/';
    $str = '$abc$
    $ab c$';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    
    var_dump($matches);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R