简体   繁体   中英

How to use pattern or matcher for replacing pattern in string?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM