简体   繁体   English

正则表达式匹配,无特定字词

[英]Regex match without specific word

I'm looking for regex that matches only the first occurrence of the expression. 我正在寻找仅与表达式的第一个匹配项匹配的正则表达式。

An expression: [B]text[/B], And I have regex for that 表达式:[B] text [/ B],为此,我使用了正则表达式

\[[B]]([a-z0-9 ./,<>()!@#$%^&*+=;:'"\[\]\\|\t\n-]*)\[\/[B]]

But that expression may occurs few times, for example: 但是该表达式可能会出现几次,例如:
[B]test1[/B] [B] TEST1 [/ B]
[B]test2[/B] [B] TEST2 [/ B]

When that happens it looks like that: 发生这种情况时,它看起来像这样:
test1[/B] TEST1 [/ B]
[B]test2 [B] TEST2

Instead of just test1 and test2 So I'm looking for some way to stop searching when it finds [/B], I know I allowed regex searching for "][/" but I want it not allowed when it's an expression "[\\B]" 不仅是test1和test2,所以我正在寻找某种在找到[/ B]时停止搜索的方法,我知道我允许正则表达式搜索“] [/”,但是当它是表达式“ [\\\\]时我不希望它B]”

Try this 尝试这个

\[B\](.*?)\[\/B\]

Here is snippet . 这是片段 Explanation here . 在这里解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM