0

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.

emilly
  • 10,060
  • 33
  • 97
  • 172

1 Answers1

0

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

revo
  • 47,783
  • 14
  • 74
  • 117
  • I don't know if OP cares but this wouldn't work for `` or similar cases. Again, don't know if OP cares, this could be good – Yassin Hajaj May 06 '18 at 10:37
  • @YassinHajaj To me *For example if code **is** code1* means `code="code1"`. I mean I don't infer *includes* from it but if you do that would be something totally different. – revo May 06 '18 at 10:43
  • @revo 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. – emilly May 06 '18 at 12:03
  • @revo see if approach 2 is easy to implement in my post if original requirement is bit trickier ? – emilly May 06 '18 at 12:08
  • @emilly You have changed whole requirements now. If you need to work around replacement string based on captured data then you will need something like this https://stackoverflow.com/questions/19737653/what-is-the-equivalent-of-regex-replace-with-function-evaluation-in-java-7 – revo May 06 '18 at 12:13
  • @revo Actually i am not getting how to capture the enclosing message tag if I know the text in code – emilly May 06 '18 at 12:29
  • If you know or if you don't know? @emilly – revo May 06 '18 at 12:30
  • I have list of possible text values that cam come under `code` attribute of message tag. I want to capture the complete message tag around that code. – emilly May 06 '18 at 12:33
  • @emilly Iterate over list items and use current regex to replace the item but instead `code(\\d+)` use the current item. – revo May 06 '18 at 12:45
  • @revo I have posted separate question for the same with clear description. Actually I first need to capture the message tag instead of straightaway implementation. Here is the https://stackoverflow.com/questions/50199958/finding-enclosing-tag. Accepting the answer based on first post. – emilly May 06 '18 at 12:58
  • @revo your suggestion to use the current item instead of` `code(\\d+)` did not work. I used regex as `String regex = "]*?\"code(\\"+textToFound+"+)\"[^>]*>"` in java. It says `Exception in thread "main" java.util.regex.PatternSyntaxException: \k is not followed by '<' for named capturing group near index 26` – emilly May 06 '18 at 13:04
  • @emilly I said instead of `code(\\d+)` not `d+`. Your other question is closed unfortunately. – revo May 06 '18 at 13:05
  • Can't I capture the enclosing message tag with the help of some readymade library (like jsoup parser or something else) or some other way ? Can't wite full parser :( – emilly May 06 '18 at 13:15