I have a string that looks like this:
我有一个看起来像这样的字符串:
Hello Hello Hello<br>Hello Hello <br> hello hello
I'm trying to capture those <br>
that are surrounded by characters using regex. So from the example string above, I should only capture the first <br>
instance, and not the second one. I tried using this:
我正在尝试使用正则表达式捕获那些被字符包围的
。所以从上面的示例字符串中,我应该只捕获第一个
实例,而不是第二个实例。我试过用这个:
\w(<br/>)\w
But I am capturing the ends this: o<br>H
但我抓住了这个目的:o
H
How can I get regex to capture only the <br>
and not the surround characters as well?
我怎样才能让正则表达式只捕获
而不是环绕字符?
1 个解决方案
#1
1
You can use look-around:
你可以使用环视:
(?<=\w)<br>(?=\w)
(I'm not sure what the /
was doing in your regex)
(我不确定你的正则表达式/正在做什么)
Though most languages allow you to extract the things you put in brackets, in which case you can leave your regex as is and just extract the first group (which would correspond to the first (and only) thing in brackets).
虽然大多数语言允许您提取放在括号中的内容,但在这种情况下,您可以保留正则表达式,只需提取第一个组(这将对应括号中的第一个(也是唯一的))。
Explanation, courtesy of this site:
解释,礼貌本网站:
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\w word characters (a-z, A-Z, 0-9, _)
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
<br> '<br>'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\w word characters (a-z, A-Z, 0-9, _)
--------------------------------------------------------------------------------
) end of look-ahead
#1
1
You can use look-around:
你可以使用环视:
(?<=\w)<br>(?=\w)
(I'm not sure what the /
was doing in your regex)
(我不确定你的正则表达式/正在做什么)
Though most languages allow you to extract the things you put in brackets, in which case you can leave your regex as is and just extract the first group (which would correspond to the first (and only) thing in brackets).
虽然大多数语言允许您提取放在括号中的内容,但在这种情况下,您可以保留正则表达式,只需提取第一个组(这将对应括号中的第一个(也是唯一的))。
Explanation, courtesy of this site:
解释,礼貌本网站:
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\w word characters (a-z, A-Z, 0-9, _)
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
<br> '<br>'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\w word characters (a-z, A-Z, 0-9, _)
--------------------------------------------------------------------------------
) end of look-ahead