简体   繁体   English

捕获具有属性和值的html标签?

[英]Capture html tag with attributes and values?

I have a complete html file input as string(I have file also) in java. 我在java中有完整的html文件输入为字符串(我也有文件)。 Text is something like below 文字如下所示

Sample input
    Some text........... <s:message code="code1" arguments="${arg1,arg2}" />..
    some text  ........
    some text  ....... <s:message code="code2" 
     />...........

Basically I need to replace all text based on code type. 基本上我需要根据代码类型替换所有文本。 For example if code is code1 then replace the s:message tag with test1 例如,如果代码是code1,则将s:message标记替换为test1

sample output
    Some text........... test1..
    some text  ........
    some text  ....... test2 ...........

I am not getting how to capture complete <s:message > and then replace it with some other text ? 我没有得到如何捕获完整的<s:message > ,然后将其替换为其他一些文本的方法? Looks like i need to use regex here but not getting how to start ? 看起来我需要在这里使用正则表达式,但不知道如何开始?

Update :- 更新:-

code1 and test1 are just examples and they can be any value. code1和test1只是示例,它们可以是任何值。 code1 can be xyz and can be replaced by abc. code1可以是xyz,可以由abc代替。 That's why i want to capture all message tags(either one by one while traversing or in one go) ,then get the code , do some logic and see what will be the replacement value. 这就是为什么我要捕获所有消息标签(遍历或一次一遍地捕获),然后获取代码,执行一些逻辑,然后看一下替换值是什么。

Approach 2:- There is another way I can do it, I have list of codes in data structure, For each code check if there is in any enclosing message tag, capture it and then process it. 方法2:-还有另一种方法可以执行,我有数据结构中的代码列表,对于每个代码,请检查是否有任何封闭的消息标签中的内容,将其捕获然后进行处理。

It seems to be XML and you would better use a parser to find the node and replace it with the text you want. 它似乎是XML,您最好使用解析器找到该节点并将其替换为所需的文本。 Doing this with Regular Expressions is rather a make-or-break (especially when your conditions go up). 使用正则表达式执行此操作很成败(特别是当您的条件上升时)。 But here is a solution for this specific problem: 但是,这是针对此特定问题的解决方案:

String regex = '<s:message\\b[^>]*?"code(\\d+)"[^>]*>';

and replace match with test$1 : 并将match替换为test$1

string = string.replaceAll(regex, "test$1");

Live demo 现场演示

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

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