0

I have a String like

"Hello @[---68---] and @[---64---] and d@[---102---] . how are you all?"

Here I want to EXTRACT numbers/strings that starts with "@[---" and ends with "---]", which here is 68,64,102 and so on.

How could I make a pattern/regex for it? Any help would be appreciated. I tried a few solutions from Pattern and Matcher

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82

2 Answers2

1

Try out this:

 Pattern MY_PATTERN = Pattern.compile("\\[(.*?)\\]");
        Matcher m = MY_PATTERN.matcher("Hello @[---68---] and @[---64---] and d@[---102---]");
         while (m.find()) {
           String s = m.group(1);
           System.out.println(s.split("---")[1]);
       }
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
0

Try below code to get numbers form String:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExamples {
    public static void main(String[]args) {
        Pattern pattern = Pattern.compile("numFound=\"([0-9]+)\"");
        Matcher matcher = pattern.matcher("");

        if (matcher.find()) {
            System.out.println(matcher.group(1));
        }
    }
}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43