duancheng7743 2014-01-23 01:20
浏览 243
已采纳

正则表达式模式匹配除最后一个之外的任何字符

I am trying to match a string using two different patterns to work together.
My source string is something like this:

Text, white-spaces, new lines and more text then ^^^^<customtag>

I need to get a group (the second one) that would capture one caret or none then a formatted HTML-like tag. So the first group would capture anything else.
It means that the string above should output this:

(Group 1)Text, white-spaces, new lines and more text then ^^^

(Group 2)^<customtag>

In the source string carets may be one, none or up to two thousands.
I need a good pattern that matches all those carets except the last one.
The code below is what I tried.

preg_match_all('/([\s\S]*\^*)(\^?<\w+>)$/', $string, $matches);


Please note: I used [\s\S] instead of the dot to match any character as well as white-spaces and new lines too.

  • 写回答

2条回答 默认 最新

  • doudun3040 2014-01-23 07:58
    关注

    You may follow the below regex:

    (?s)(.*)((\^|(?<!\^))<[^>]+>)
    

    Live demo

    PHP code:

    preg_match_all('/(?s)(.*)((\^|(?<!\^))<[^>]+>)/', $string, $matches);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题