Tom*_*tny 5 python java regex parsing
我有一个非常简单的输入字符串模式 - 大写字母,整数,大写字母,整数,...我想分隔每个大写字母和每个整数.我无法弄清楚在Java中执行此操作的最佳方法.
我已经使用Pattern和Matcher,然后使用StringTokenizer尝试了regexp,但仍然没有成功.
这就是我想要做的,用Python表示:
for token in re.finditer( "([A-Z])(\d*)", inputString):
print token.group(1)
print token.group(2)
Run Code Online (Sandbox Code Playgroud)
对于输入"A12R5F28",结果将是:
A
12
R
5
F
28
Run Code Online (Sandbox Code Playgroud)
您可以在Java中使用regex API并实现相同的功能:
Pattern myPattern = Pattern.compile("([A-Z])(\d+)")
Matcher myMatcher = myPattern.matcher("A12R5F28");
while (myMatcher.find()) {
// Do your stuff here
}
Run Code Online (Sandbox Code Playgroud)