1

我有兴趣将数字放入String's 数组中,但无法很好地使用正则表达式和组。

代码:

package regex;

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

public class Regex {

    public static void main(String[] args) {
        String s = "A: 123/456  BC: 78/90  NEw: 105 E: 0  BK: 10 None: 0%";
        Pattern p = Pattern.compile("(\\w+): (\\d+)");
        Matcher m = p.matcher(s);
        String[] as = new String[7];
        as[2] = m.group();  //array list??
        for (int i = 2; i < 2; i++) {
            System.out.println(as[i]);
        }
    }
}
4

4 回答 4

1

Matcher无法按照您尝试使用它的方式工作。该group()方法返回最后一个匹配的组。要真正告诉匹配器匹配某些东西,您需要先调用find()find()返回一个布尔值,告诉匹配器是否设法找到下一个匹配项,以便您可以像这样循环所有匹配项:

while (m.find()) {
    String text = m.group();
    // ...
}
于 2013-09-02T09:30:13.733 回答
1

你可以试试正则表达式:

(\w+): +(\S+)

正则表达式可视化

例如:

private static final Pattern REGEX_PATTERN = 
        Pattern.compile("(\\w+): +(\\S+)");

public static void main(String[] args) {
    String input = "A: 123/456  BC: 78/90  NEw: 105 E: 0  BK: 10 None: 0%";
    Matcher matcher = REGEX_PATTERN.matcher(input);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}

输出:

A: 123/456
BC: 78/90
NEw: 105
E: 0
BK: 10
None: 0%

如果您想获取名称,matcher.group(1)号码, 使用matcher.group(2).

private static final Pattern REGEX_PATTERN =
        Pattern.compile("(\\w+): +(\\S+)");

public static void main(String[] args) {
    String input = "A: 123/456  BC: 78/90  NEw: 105 E: 0  BK: 10 None: 0%";
    Matcher matcher = REGEX_PATTERN.matcher(input);
    while (matcher.find()) {
        System.out.println("Name: " + matcher.group(1));
        System.out.println("Number: " + matcher.group(2));
    }
}

输出:

Name: A
Number: 123/456
Name: BC
Number: 78/90
Name: NEw
Number: 105
Name: E
Number: 0
Name: BK
Number: 10
Name: None
Number: 0%
于 2013-09-02T17:14:17.797 回答
1

你需要改变

Pattern p = Pattern.compile("(\\w+): (\\d+)");

进入

Pattern p = Pattern.compile("(\\w+): (\\d+/{0,1}\\d*%{0,1})");

于 2013-09-02T09:27:58.933 回答
0

只是想我会把这个扔出去做一个反应:

package regex;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {

    public static void main(String[] args) {
        List<Map.Entry> l = new ArrayList<>();
        String monitor = "A: 123/456  BC: 78/90  NEw: 105 E: 0  BK: 10 None: 0%";
        Pattern outerPattern = Pattern.compile("(\\w+): (\\d+)");
        Matcher outerMatcher = outerPattern.matcher(monitor);
        String outerGroup = null;
        int i = 0;
        Pattern innerPattern;
        Matcher innerMatcher;
        String innerGroup = null;
        while (outerMatcher.find()) {
            outerGroup = outerMatcher.group();
            System.out.println(outerGroup);
            innerPattern = Pattern.compile("(\\w+): (\\d+)");
            innerMatcher = innerPattern.matcher(outerGroup);
            while (innerMatcher.find()) {
                innerGroup = innerMatcher.group();
                System.out.println(innerGroup);
                Map.Entry<String, Integer> entr = new AbstractMap.SimpleEntry<>("exmpleString", 42);
                l.add(entr);
            }
        }
    }
}
于 2013-09-02T10:22:15.880 回答