1

I've been looking all night but none of the other answers satify my needs. They don't even run http://regexr.com/ . What i'm looking is to replace all between <whatever> replace this stuff </whatever>

public void updateValue(String tag, String value) {
    content = content.replaceAll("<"+tag+">(.?+)</"+tag+">", value);
}

public void updateValue(String tag, int value) {
    content = content.replaceAll("<"+tag+">(.?+)</"+tag+">", value+"");
}

public void updateValue(String tag, File value) {
    content = content.replaceAll("(&<"+tag+">=)[^&]*(&</"+tag+">=)", value.getPath());      
}

None of them work at all.

EDIT

Current approach, tags are beign removed along with it's content by regex.

private void replaceValue(String tag, String value) {
    content = content.replaceAll("<"+tag+">(.*?)<\\/"+tag+">", "<"+tag+">"+value+"</"+tag+">");
}
2
  • How don't they satisfy your needs? .?+ is incorrect syntax as that site tells you. What are your needs anyway? Commented Jul 31, 2017 at 7:53
  • what is the expected result? Commented Jul 31, 2017 at 7:57

2 Answers 2

1

It not work because the pattern you are using (.?+) is a little weird, it means match any character . optional ? one or more time + check your regex here, to solve your problem you have to make some changes you can use:

content = content.replaceAll("<"+tag+">(.+?)<\\/"+tag+">", value);
//--------------------------------------^^^---^^

So you have to use (.+?), (.?+), and you have to espace the / with \/.

check the regex demo


Edit

In the second case you have to use patterns with groups, so you have to match the first tag name then the value between the tags then the closed tag, you can use this :

private static void replaceValue(String tag, String value) {
    String regex = "<(.*?)>(.*?)</(.*?)>";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(content);

    if (matcher.find()) {                                                
        content = content.replaceAll(matcher.group(1), tag)
                .replaceAll(matcher.group(2), value)
                .replaceAll(matcher.group(3), tag);
    }
    System.out.println(content);
}

In case of

String content = "<MySQL.port>3306</MySQL.port>";

and tag = "tag_name", value = "XXXX", the reslt is :

<tag_name>XXXX</tag_name>
Sign up to request clarification or add additional context in comments.

7 Comments

It doesn't work with empty content between tags, and its removing tags aswell. (i can add'em manually if necessary). EDIT replacing + by * solved the empty case, but tags are still beign removed.
@WesosdeQueso what should the output look like?
<MySQL.port>3306</MySQL.port> to <MySQL.port>XXXX</MySQL.port>, for example. It's working but i have to rewrite both <MySQL.port>and </MySQL.port> tags. It's doing : <MySQL.port>3306</MySQL.port> to XXXX
so <MySQL.port> can be any thing else or not?
I think i'm gonna stick with the current one i have. Thanks for your help.
|
1
content = content.replaceAll("<tag>(.*?)</tag>", value);

Edited answer for accepting tag from parameter:

content = content.replaceAll("<"+tag"+">(.*?)</"+tag+">", value);

4 Comments

This will replace tag name. It's not what OP is looking for.
check edited ans
Now it will replace only tags named tag. OP wants to have ability to replace content of any tag.
Accept that tag as some value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.