0

I have a string that I want to split into an array:

SEQUENCE: 1A→2B→3C

I tried the following regular expression:

((.*\s)|([\x{2192}]*))

1. \x{2192} is the arrow mark
2. There is a space after the colon, I used that as a reference for matching the first part

and it works in testers(Patterns in OSX) enter image description here

but it splits the string into this:

[, , 1, A, , 2, B, , 3, C]

How can I achieve the following?:

[1A,2B,3C]

This is the test code:

String str = "SEQUENCE: 1A→2B→3C"; //Note that there's an extra space after the colon
System.out.println(Arrays.toString(str.split("(.*\\s)|([\\x{2192}]*)")));
4

2 回答 2

5

正如 Richard Sitze 的帖子中所指出的,正则表达式的主要问题是它应该使用+而不是*. 此外,您还可以对正则表达式进行进一步改进:

  • 而不是\\x{2192},使用\u2192. 而且因为是单个字符,所以不需要放到字符类([...])中,直接使用\u2192+即可。
  • 此外,由于|绑定比.*\\sand更松散\u2192+,因此您也不需要那里的括号。所以你的最终表达是简单的".*\\s|\u2192+"
于 2013-06-22T18:46:31.157 回答
5

\u2192*匹配 0 个或多个箭头 - 这就是为什么要拆分每个字符(拆分空字符串)。尝试更改*+.

于 2013-06-22T18:49:41.677 回答