Toney
Toney

Reputation: 25

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);

Upvotes: 0

Views: 758

Answers (2)

J0e3gan
J0e3gan

Reputation: 8938

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.

Upvotes: 2

drieschr
drieschr

Reputation: 1

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.

Upvotes: 0

Related Questions