-1

我有一个文件,其中的行具有字符串模式

(digit)(one-or-more-space-seperator)(word-possibly-starting-and-ending-with-@)(one-or-more-space-seperator)(more-words-seperated-by-spaces)

例如:

1 NAME first name

2  NAME   last name

3 @silly@  i am nuts

4 @lilly@

上面每一行的正则表达式匹配器的结果输出应分别如下:

[1, NAME, first name]

[2, NAME, last name]

[3, @silly@, i am nuts]

[4, @lilly@]

如果你成功找到了一个正则表达式,我想知道你是如何制作一个..

当涉及到正则表达式时,我陷入了困境:(

非常感谢您的帮助!

4

2 回答 2

2

像这样的东西?

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

class Module1{
    public static void main(String[] asd){
        String sourcestring = "1 NAME john smith";
        Pattern re = Pattern.compile("(\\d+)\\s+(NAME|@\\w+@)(.*)");
        Matcher m = re.matcher(sourcestring);
        int i = 0;
        while (m.find()){
            for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
                System.out.println( "[" + i + "][" + groupIdx + "] = " + m.group(groupIdx));
            }
            i++;
        }
    }
}
于 2013-11-07T18:56:51.763 回答
1

(\d+) +(\w+|@\w+@) *([\w ]+)?应该做的伎俩。(替换为 Java 的双转义符)

于 2013-11-07T18:55:06.743 回答