查找封闭标签包含特定文本?

我在java中输入了一个完整的html文件作为字符串(我也有文件)。 文字如下所示

Sample input Some text........... .. some text ........ some text ....... ........... 

以下是我需要付费的步骤

  1. 我列出了准备好的代码列表。 我希望迭代列表中的每个代码,然后在代码匹配时捕获该代码周围的封闭消息标记。
  2. 然后根据代码值做一些逻辑。
  3. 替换具有新值的消息。

基本上我需要根据代码类型替换所有文本 。 例如,如果代码是code1,则用test1替换s:message标记

 sample output Some text........... new text.. some text ........ some text ....... new text........... 

我没有得到如何做第1步,即捕获封闭的消息标签?

这是匹配代码所需的正则表达式。
如果你想要它是智能的,你需要回调
即基于代码 ,它位于捕获组2中。

为了替换目的,整个匹配是标记。

原始正则表达式:
"']|"[^"]*"|'[^']*')*?\scode\s*=\s*(?:(['"])([\S\s]*?)\1))\s+(?:"[\S\s]*?"|'[\S\s]*?'|[^>]*?)+/>

弦乐正则表达式:
"\"']|\"[^\"]*\"|'[^']*')*?\\scode\\s*=\\s*(?:(['\"])([\\S\\s]*?)\\1))\\s+(?:\"[\\S\\s]*?\"|'[\\S\\s]*?'|[^>]*?)+/>"

测试: https : //regex101.com/r/LgweAW/1


请注意,如果要搜索特定的代码
像1,4,22,9,在正则表达式中,只需替换这一行

( [\S\s]*? ) # (2), The Code

与你的特定正则表达式,像这样

( (?:1|4|22|9) ) # (2), One of these Codes


可读版本:

  # Begin Message tag < s:message (?= \s ) (?= # Asserttion (a pseudo atomic group) (?: [^>"'] | " [^"]* " | ' [^']* ' )*? \s code \s* = \s* (?: ( ['"] ) # (1), Quote ( [\S\s]*? ) # (2), The Code \1 ) ) # Have the code, just match the rest of tag \s+ (?: " [\S\s]*? " | ' [\S\s]*? ' | [^>]*? )+ /> # End self contained tag