我有一些正则表达式用于在标记之间放置内容,如结果所示。如果我在结果文本上应用相同的正则表达式,我将在标签内部获得标签...
原始内容:
Lorem ipsum 123456 dolor坐@twitter amet, 这是一个很好的例子。
结果:
Lorem ipsum [tel] 123456 [/ tel] dolor sit [tw] @twitter [/ tw] amet,consectetur adipiscing elit [a] example [/ a]。
结果第二次:
Lorem ipsum [tel] [tel] 123456 [/ tel] [/ tel] dolor sit [tw] [tw] @twitter [/ tw] [/ tw] amet,consectetur adipiscing elit [a] [a]示例[/ a] [/ a]。
如果内容介于任何[]和[/]之间,那么我的正则表达式中的内容是否会匹配?
答案 0 :(得分:1)
(?:[0-9]+|twitter|consectetur)(?![0-9a-z]*\[\/[a-z]+\])
替换为:[xx]$0[/XX]
此正则表达式将执行以下操作:
twitter
和单词consectetur
。我选择了这些子串来说明正则表达式,但这些可以用其他字符串替换。[0-9+]
将匹配源字符串中的2345
,但它可能已经被标记包装twitter
而前导@
仍然有一个尾随标记现场演示
https://regex101.com/r/lW2pY6/1
示例文字
123456 Lorem ipsum [tel] 123456 [/ tel] dolor sit [tw] @twitter [/ tw] amet,consectetur adipiscing elit [a] example [/ a]
替换后的样本
[XX] 123456 [/ XX] Lorem ipsum [tel] 123456 [/ tel] dolor sit [tw] @twitter [/ tw] amet,[XX] consectetur [/ XX] adipiscing elit [a]例子[/ A]
NODE EXPLANATION
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
twitter 'twitter'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
consectetur 'consectetur'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
[0-9a-z]* any character of: '0' to '9', 'a' to 'z'
(0 or more times (matching the most
amount possible))
----------------------------------------------------------------------
\[ '['
----------------------------------------------------------------------
\/ '/'
----------------------------------------------------------------------
[a-z]+ any character of: 'a' to 'z' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
\] ']'
----------------------------------------------------------------------
) end of look-ahead