0

I have the string as follows :

SUB8&20.000,-&succes&09/12/18SUB12&100.000,-&failed&07/12/18SUB16&40.000,-&succes&09/12/18

I want to get a string "8&20.000","16&40.000" between SUB and ,-&succes


I want to get succes data how to get the string using java regex ?

Rona Idea
  • 6,734
  • 2
  • 9
  • 12

3 Answers3

2

Use this regex,

SUB([^,]*),-&succes

Java code,

public static void main(String[] args) {
    String s = "SUB8&20.000,-&succes&09/12/18SUB12&100.000,-&failed&07/12/18SUB16&40.000,-&succes&09/12/18";
    Pattern p = Pattern.compile("SUB([^,]*),-&succes");
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1));
    }
}

Prints,

8&20.000
16&40.000

Check here

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • sorry I just updated the question because there is something missing, is this still working on my latest question? – Rona Idea Dec 10 '18 at 04:53
1

You could use the pattern SUB[^S]+&success[^S]+ and choose the one you want after that.

The two match would be SUB8&20.000,-&succes&09/12/18 and SUB16&40.000,-&succes&09/12/18.

Once you have chosen you can strip away the unwanted stuff with [0-9]+&[0-9.]+.

Dominique Fortin
  • 2,212
  • 15
  • 20
1

I don`t know if I am answering your question properly or not. But this regex will give exact string that you are looking for.

(?<=SUB)([^,]*)(?=,-&succes) 

https://regex101.com/r/RLFXNf/1

Prashant Deshmukh.....
  • 2,244
  • 1
  • 9
  • 11