简体   繁体   中英

How is this regex splitting this string?

I'm having a problem unraveling some Java code sent me. How is this regular expression splitting out the strings with the array?

String[] words = haystack.split("[ \"\'\t\n\b\f\r]", 0);

It is splitting on any of the following in the character class denoted by the opening [ and closing ] :

  • – space
  • \\" – quote
  • \\' – apostrophe
  • \\t – tab
  • \\n – line feed
  • \\b – backspace
  • \\f – form feed
  • \\r – carriage return

See the Java regex reference for clarification about regex character classes and characters.

Also, note that \\' could very well be ' , escaping being unnecessary in that case. Check out an example split on Ideone with this little change.

I think you mean haystring (which is a string) is split into an array of strings. All white space characters are used as a delimeter.

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