3

I need some help to extract multiple substrings from a string. An example of a string is as given below:

String str = "What is <Mytag a exp 5 exp 3> written as a single power of <i>a</i> <Mytag yx4> and the double power of <b>x+y</b> <Mytag 3xy4>";

I am trying to get substrings Between "<Mytag" and ">"

so my desire output will be
1) a exp 5 exp 3
2) yx4
3) 3xy4

I have tried with Scanner and substring everything I getting first string successfully but the problem with getting second and third occurrence.

in substring methods I am successfully getting index of all tages "<Mytag" but not able to get correct index of ">" because it comes with Bold and italic also.

3
  • You can try regex processing of the string Commented Dec 3, 2012 at 6:31
  • Getting the closest > after each <mytag should work, am I right? Commented Dec 3, 2012 at 6:31
  • indexOf() can also be written in this form int indexOf(int ch, int startIndex) . Here you can specify the startIndex to be 1 more than the previous index found. Last you should also check of IndexOutOfBounds exception on performing these actions Commented Dec 3, 2012 at 6:35

2 Answers 2

4

Use Regex for that: -

"<Mytag ([^>]*)>"

And get the group 1 from the above regex. You need to use it with Pattern and Matcher class, and use Matcher#find method with a while loop to find all the matching substrings.

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

Comments

3

As Rohit Jain said, with regular expressions. Here is functional code:

// import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexTestHarness {

  public static void main(String[] args){
    // Console console = System.console();  // Not needed

    Pattern pattern = Pattern.compile("<Mytag([^>]*)>");

    String myString = "What is <Mytag a exp 5 exp 3> written as a single power of <i>a</i> <Mytag yx4> and the double power of <b>x+y</b> <Mytag 3xy4>";
    Matcher matcher = pattern.matcher(myString);

    while (matcher.find()) {
      // Rohit Jain observation
      System.out.println(matcher.group(1));
    }

  }
}

Source: Java Regex tutorial.

3 Comments

But why are you using replace? Can't you just get the group 1?
Really? I did not know. I know about the backwhack but in Java I never used it.
Ok, I will take it in account. Thanks.

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.