3

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@"));
4
  • 5
    You could use something like String#split and pass it \(D@[0-9]+\) as the regular expression, this would at least allow you to get the data between the (@D*) blocks... Commented Jul 10, 2015 at 5:41
  • @MadProgrammer but no indicator showing which one is for 1 or 2 or 3 Commented Jul 10, 2015 at 5:47
  • Use with this overload of indexOf public int indexOf(String str, int fromIndex); Commented Jul 10, 2015 at 5:51
  • 2
    Then use the Matcher API which will give more information... Commented Jul 10, 2015 at 5:54

5 Answers 5

2

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.

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

Comments

2

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 ;)

Comments

1

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 

Comments

0

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]+\)");

Comments

0

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

Comments

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.