简体   繁体   中英

extra space after parsing a string with regular expression

I have the following simple code:

String d = "_|,|\\.";
String s1 = "b,_a_.";

Pattern p = Pattern.compile(d);
String[] ss = p.split(s1);
for (String str : ss){
     System.out.println(str.trim());
}

The output gives

b

a

Where does the extra space come from between b and a?

You do not have an extra space, you get an empty element in the resulting array because your regex matches only 1 character, and when there are several characters from the set on end, the string is split at each of those characters.

Thus, you should match as many of those characters in your character class as possible with + (1 or more) quantifier by placing the whole expression into a non-capturing group ( (?:_|,|\\\\.)+ ), or - better - using a character class [_,.]+ :

String d = "(?:_|,|\\.)+"; // Or better: String d = "[_,.]+";
String s1 = "b,_a_.";
Pattern p = Pattern.compile(d);
String[] ss = p.split(s1);
for (String str : ss){
    System.out.println(str.trim());
}

See IDEONE demo

当我感到困惑时,也许您想要的是将正则表达式更改为

String d = "[_,\\.]+";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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