1

I want select all the text between <h3> and /h3> <. After selection I like to replace the value of my String with the result. In the following example the result should be Basic Information

String test="<h3>Basic Information</h3> <div>";
test = test.replaceAll("<h3>(.*?)</h3>", "$1");

But at the moment the result is

Basic Information <div>

1
  • 1
    I don't see why you'd expect to get only "Basic Information" as a result with this code. Are you sure that replaceAll is really what you want? Perhaps you rather need to extract the match instead of replacing it. Commented May 15, 2016 at 10:22

2 Answers 2

4

With regex you can do:

String test="<h3>Basic Information</h3> <div>";
String repl = test.replaceFirst(".*<h3>([^&]+).*/h3> <.*", "$1");
//=> Basic Information

Though you can avoid regex altogether and use String APIs to extract same text as well.

Alternatively you can use this regex for matching:

<h3>([^&]+).*/h3> <

and grab captured group #1 using Pattern and Matches APIs.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, one example would be to use String splits just for performance sake. Unless the use if for something bigger. These are actually xml snippets so if they become big enough maybe a SAX parser could do the job better.
1

Try this:

Pattern pattern = Pattern.compile("<h3>(.*)<\\/h3>");
Matcher matcher = pattern.matcher("<h3>Basic Information</h3> <div>");
matcher.find();
StringBuffer sb = new StringBuffer();
matcher.appendReplacement(sb,"$1");
String result = sb.toString();

The reason why you cannot do that with just replaceFirst it's because the appendTail method is called at the end of the replaceFirst method. The matcher will replace the groups you did not specified with empty,the groups you did specified with their value and of course the non-matching bits which well, since no match was created for them, they do not get replaced at all.

In the case of your query:

group 0: <h3>

group 1: Basic Information

group 0: </h3>

non-match: <div>

This is just a generic example of what you can do with the matchers. Of course if you just want the group in specific... Well just use:

matcher.group(1)

6 Comments

Why use the StringBuffer instead of just calling group(1)? And sb.toString; is not valid java.
This is just an example. Of course you can use group for it. I mean if you just want to replace groups without having the unmatched Strings about then StringBuffer is still better. If you just want one specific group... well just get the group. I just edited it.
@RealSkeptic btw, care to explain what do you mean by sb.toString() is not Java? AFAIK, StringBuffer contents are converted to string by the toString() method.
@RealSkeptic Another thing that you can do is to look at the API to see if sb.toString() is java or not: @Override public synchronized String toString() { if (toStringCache == null) { toStringCache = Arrays.copyOfRange(value, 0, count);} return new String(toStringCache, true);}... when I look at the API it looks prety valid java hey :)
It's not valid Java without parentheses. You wrote sb.toString instead of sb.toString(), and you weren't returning the value anywhere (which is legal but meaningless).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.