相关疑难解决方法(0)

Java正则表达式首先匹配

如何告诉以下正则表达式只找到FIRST匹配?以下代码继续在字符串中查找所有可能的正则表达式.

即我只寻找子串的索引 (200-800;50]

public static void main(String[] args) {

    String regex = "(\\[|\\().+(\\]|\\))";

    String testName=  "DCGRD_(200-800;50]MHZ_(PRE|PST)_(TESTMODE|REG_3FD)";

            Pattern pattern = 
            Pattern.compile(regex);

            Matcher matcher = 
            pattern.matcher(testName);

            boolean found = false;

            while (matcher.find()) {
                System.out.format("I found the text" +
                    " \"%s\" starting at " +
                    "index %d and ending at index %d.%n",
                    matcher.group(),
                    matcher.start(),
                    matcher.end());
                found = true;

            }

            if (!found){
                System.out.println("Sorry, no match!");
            }
}
Run Code Online (Sandbox Code Playgroud)

java regex string

11
推荐指数
1
解决办法
3万
查看次数

标签 统计

java ×1

regex ×1

string ×1