Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用正则表达式从足球位置数组中获取数字_Java_Regex - Fatal编程技术网

Java 使用正则表达式从足球位置数组中获取数字

Java 使用正则表达式从足球位置数组中获取数字,java,regex,Java,Regex,我需要一些帮助来读取位置的位置,这样我就可以使用它们为我正在创建的足球应用程序创建文本字段面板。不完全确定下一步该做什么 String[] positions = {"4-4-2", "4-3-3", "3-5-2", "5-3-2", "3-4-3", "4-5-1"}; JComboBox select = new JComboBox(positions); this.add(select, BorderLayout.NORTH); select.addAc

我需要一些帮助来读取位置的位置,这样我就可以使用它们为我正在创建的足球应用程序创建文本字段面板。不完全确定下一步该做什么

    String[] positions = {"4-4-2", "4-3-3", "3-5-2", "5-3-2", "3-4-3", "4-5-1"};

    JComboBox select = new JComboBox(positions);
    this.add(select, BorderLayout.NORTH);
    select.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            select.getItemAt(select.getSelectedIndex());
            Pattern p = Pattern.compile("\\d\\-\\d\\-\\d");
            Matcher m = p.matcher(select.toString());
            m.find();
            System.out.println();
        }
    });
    setVisible(true);

如果你真的想要正则表达式,你可以这样做:

Pattern p = Pattern.compile("(\\d)\\-(\\d)\\-(\\d)");
Matcher m = p.matcher(select.toString());
if(m.find())
{
    System.out.println( "Position 1 is : " + m.group(1) );
    System.out.println( "Position 2 is : " + m.group(2) );
    System.out.println( "Position 3 is : " + m.group(3) );
}

但是,可以使用split()。

“4-4-2”。split(“-”)非常有效。为什么要把它弄得更复杂?@cricket\u 007 Nit:。你需要什么样的输出?你需要得到4,4,2吗?@svasa是这样的,这样我就可以得到这些值并在字段中使用它们了为什么不只是把它们作为
int[]
<代码>int[][]位置={{4,4,2},{4,3,3}等}