繁体   English   中英

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

[英]Splitting a string composed of numbers and alphabets

我想打断像这样的字符串:

String s = "xyz213123kop234430099kpf4532";

转换成令牌,每个令牌以字母开头,以数字结尾。 因此,以上字符串可以分为3个令牌:

xyz213123
kop234430099
kpf4532

该字符串s可能非常大,但模式将保持不变,即每个令牌都将以3个字母开头并以一个数字结尾。

如何拆分它们?

尝试这个:

\w+?\d+

Java Matcher

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);
}

对于未来,这是:

正则表达式

像这样做,

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM