简体   繁体   中英

Capture html tag with attributes and values?

I have a complete html file input as string(I have file also) in java. 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

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 ? 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 can be xyz and can be replaced by 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.

It seems to be XML and you would better use a parser to find the node and replace it with the text you want. 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 :

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

Live demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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