Muhammad Umar
Muhammad Umar

Reputation: 11782

Get substring in a string with multiple occurring string

I have a string something like

(D@01)5(D@02)14100319530033M(D@03)1336009-A-A(D@04)141002A171(D@05)1(D@06)

Now i want to get substring between (D@01)5(D@02)

If i have something like

(D@01)5(D@02)

i can get detail with

    quantity         = content.substring(content.indexOf("(D@01)") + 6, content.indexOf("(D@02)"));

But somethings D@02 can be different like @05, Now how can i use simple (D@ to get string in between. there are multiple repetitions of (D@

Basically this is what i want to do

content.substring(content.indexOf("(D@01)") + 6, content.nextOccurringIndexOf("(D@"));

Upvotes: 3

Views: 150

Answers (5)

dting
dting

Reputation: 39287

You can use regex capture groups if want the content between the (D@##)'s

Pattern p = Pattern.compile("(\\(D@\\d+\\))(.*?)(?=\\(D@\\d+\\))");
Matcher matcher = p.matcher("(D@01)5(D@02)14100319530033M(D@03)1336009-A-A(D@04)141002A171(D@05)1(D@06)");
while(matcher.find()) {
  System.out.println(String.format("%s start: %2s end: %2s matched: %s ",
      matcher.group(1), matcher.start(2), matcher.end(2), matcher.group(2)));
}

(D@01) start:  6 end:  7 matched: 5 
(D@02) start: 13 end: 28 matched: 14100319530033M 
(D@03) start: 34 end: 45 matched: 1336009-A-A 
(D@04) start: 51 end: 61 matched: 141002A171 
(D@05) start: 67 end: 68 matched: 1 

Upvotes: 1

PC Luddite
PC Luddite

Reputation: 6088

Try this:

public static void main(String[] args) {
    String input = "(D@01)5(D@02)14100319530033M(D@03)1336009-A-A(D@04)141002A171(D@05)1(D@06)";
    Pattern p = Pattern.compile("\\(D@\\d+\\)(.*?)(?=\\(D@\\d+\\))");
    Matcher matches = p.matcher(input);
    while(matches.find()) {
        int number = getNum(matches.group(0)); // parses the number
        System.out.printf("%d. %s\n", number, matches.group(1)); // print the string
    }
}

public static int getNum(String str) {
    int start = str.indexOf('@') + 1;
    int end = str.indexOf(')', start);
    return Integer.parseInt(str.substring(start,end));
}

Result:

1. 5
2. 14100319530033M
3. 1336009-A-A
4. 141002A171
5. 1

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347234

Assuming that the marker and value are some how linked and you want to know each ((D@01) == 5), then you can make use of the Pattern/Matcher API, for example

String text = "(D@01)5(D@02)14100319530033M(D@03)1336009-A-A(D@04)141002A171(D@05)1(D@06)";
Pattern p = Pattern.compile("\\(D@[0-9]+\\)");
Matcher m = p.matcher(text);

while (m.find()) {
    String name = m.group();
    if (m.end() < text.length()) {
        String content = text.substring(m.end()) + 1;
        content = content.substring(0, content.indexOf("("));
        System.out.println(name + " = " + content);
    }
}

Which outputs

(D@01) = 5
(D@02) = 14100319530033M
(D@03) = 1336009-A-A
(D@04) = 141002A171
(D@05) = 1

Now, this is a little heavy handed, I'd create some kind of "marker" object which contained the key (D@01) and it's start and end indices. I'd then keep this information in a List and cut up each value based on the end of the earlier key and the start of the last key...but that's just me ;)

Upvotes: 2

Shreevardhan
Shreevardhan

Reputation: 12641

I suppose you can do

int fromIndex = content.indexOf("(D@01)") + 6;
int toIndex = content.indexOf("(D@", fromIndex);    // next occurring

if (fromIndex != -1 && toIndex != -1)
    str = content.substring(fromIndex, toIndex);

Output

5

See http://ideone.com/RrUtBy demo.

Upvotes: 2

ttarczynski
ttarczynski

Reputation: 1014

You can user regex to split the input - as suggested by @MadProgrammer. split() method produces a table of Strings, so the order of the occurrences of the searched values will be exactly the same as the order of the values in the table produced by split(). For example:

String input = "(D@01)5(D@02)14100319530033M(D@03)1336009-A-A(D@04)141002A171(D@05)1(D@06)";

String[] table = input.split("\(D@[0-9]+\)");

Upvotes: 0

Related Questions