拆分由数字和字母组成的字符串

时间:2013-10-29 07:22:53

标签: java

我想打破一个字符串:

String s = "xyz213123kop234430099kpf4532";

到令牌中,每个令牌以字母开头,以数字结尾。所以上面的字符串可以分解为3个标记:

xyz213123
kop234430099
kpf4532

此字符串s可能非常大,但模式将保持不变,即每个标记将以3个字母开头并以数字结尾。

如何分割它们?

3 个答案:

答案 0 :(得分:2)

试试这个:

\w+?\d+

Java 匹配

Pattern pattern = Pattern.compile("\\w+?\\d+"); //compiles the pattern we want to use
Matcher matcher = pattern.matcher("xyz213123kop234430099kpf4532"); //we create the matcher on certain string using our pattern

while(matcher.find()) //while the matcher can find the next match
{
    System.out.println(matcher.group()); //print it
}

然后你可以使用 Regex.Matches C#:

foreach(Match m in Regex.Matches("xyz213123kop234430099kpf4532", @"\w+?\d+"))
{
    Console.WriteLine(m.Value);
}

对于未来这个:

RegExr

答案 1 :(得分:2)

这样做,

String s = "xyz213123kop234430099kpf4532";

    Pattern p = Pattern.compile("\\w+?\\d+");
    Matcher match = p.matcher(s);
    while(match.find()){
        System.out.println(match.group());  
    }

输出

xyz213123
kop234430099
kpf4532

答案 2 :(得分:1)

你可以从这样的正则表达式开始:(\ w +?\ d +) http://regexr.com?36utt

相关问题