简体   繁体   中英

Splitting String by multiple symbols,regex

Regex split through multiple symbols String s="He is a very very good boy, isn't he?"

String[] sa = s.split("[!, ?._'@]");
System.out.println(sa.length);
for (String string : sa) {
    System.out.println(string);
}

11
He
is
a
very
very
good
boy

isn
t
he

while using

String[] sa = s.split("[!, ?._'@]+");

10
He
is
a
very
very
good
boy
isn
t
he

+ in regex ie used for one or more but how this space is coming?

This happens because the split function is creating an array element containing an empty string between the comma , and the spaceafter boy .

arr = ['He', 'is', 'a', 'very', 'very', 'good', 'boy', '', 'isn', 't', 'he']

The function beleives there is some text between the comma and the space when it splits the text, effectively generating that empty string.

When you use the + symbol, you split by "groups" of characters, and it takes the comma and space as the splitting regular expression, not generating that empty string between those characters.

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