0

使用正则表达式我必须拆分字符串模式并展开它们。

例如:

String Pattern = "rue Richard2" -----> in 2 string 
Substring1 = "rue Richard" , Substring2 = "2"

但是对于像“rue-Richard 2-5”这样更复杂的字符串模式,我必须拆分和扩展:

Substring1 = "rue-Richard " , Substring2 = "2"
Substring1 = "rue-Richard " , Substring2 = "3"
Substring1 = "rue-Richard " , Substring2 = "4"
Substring1 = "rue-Richard " , Substring2 = "5"

但对于另一个更复杂的字符串模式,如“rue-Richard 2,5,7,11”,我必须拆分和展开:

Substring1 = "rue-Richard." , Substring2 = "2"
Substring1 = "rue-Richard." , Substring2 = "5"
Substring1 = "rue-Richard." , Substring2 = "7"
Substring1 = "rue-Richard." , Substring2 = "11"
4

1 回答 1

0

这里的正则表达式无济于事,因为您有两个任务要做:

  • 将非数字与数字分开;
  • 分析数字。

一个简单的解决方案是使用Guava'sCharMatcher

// DIGIT matches all unicode digits, we don't want that
private static final CharMatcher DIGITS = CharMatcher.DIGIT
    .and(CharMatcher.ASCII);

// find first digit index
final int index = DIGITS.indexIn(input);
input.substring(0, index); // part without the digits
input.substring(index); // part with digits

然后你“只”必须分析数字部分。

于 2013-07-04T08:16:19.627 回答