我正在尝试设计一个Perl正则表达式作为if
条件来标识|但如果|包含在带引号的字符串中的任何位置,如'It went〉|碰撞|当它掉在地板上
输入示例如下
这应匹配:
action 71|55|279|286|155|57|343
这不应匹配:
action mud_destroyset($me,$arg,$arg1,$arg2,'gun','2','There is an almighty >| CRASH |< . When the smoke clears, both door and sphere are gone...','You hear the >| CRASH |< of a cannon going off in the distance.','',0,$cid,$oc) ;
我试过如下的negative lookbehind regex,广泛地摆弄,失败了。我甚至问了ChatGPT,它也失败了。
这些没有工作(第一个是ChatGPT解决方案,第二个是我的尝试):
^(?:(?!'\|).)*\|
(?<!').+\|.+
https://regex101.com/r/1o0SOM/1
https://regex101.com/r/z5Xz83/1
感谢帮助!
1条答案
按热度按时间pu82cl6c1#
一种方法是非破坏性地删除单引号中的任何内容(包括引号),然后查找管道:
运行示例:
它给出了
'
:文字单引号[^']*
:除单引号外的任何内容,尽可能多次重复'
:还是一个单引号index
函数查找子字符串(“|“),如果在搜索的字符串中找不到它,则返回-1。