Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 42753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:23:15+00:00 2026-05-10T15:23:15+00:00

Suppose you have the following string: white sand, tall waves, warm sun It’s easy

  • 0

Suppose you have the following string:

white sand, tall waves, warm sun 

It’s easy to write a regular expression that will match the delimiters, which the Java String.split() method can use to give you an array containing the tokens ‘white sand’, ‘tall waves’ and ‘warm sun’:

\s*,\s* 

Now say you have this string:

white sand and tall waves and warm sun 

Again, the regex to split the tokens is easy (ensuring you don’t get the ‘and’ inside the word ‘sand’):

\s+and\s+ 

Now, consider this string:

white sand, tall waves and warm sun 

Can a regex be written that will match the delimiters correctly, allowing you to split the string into the same tokens as in the previous two cases? Alternatively, can a regex be written that will match the tokens themselves and omit the delimiters? (Any amount of white space on either side of a comma or the word ‘and’ should be considered part of the delimiter.)

Edit: As has been pointed out in the comments, the correct answer should robustly handle delimiters at the beginning or end of the input string. The ideal answer should be able to take a string like ‘,white sand, tall waves and warm sun and ‘ and provide these exact three tokens:

[ 'white sand', 'tall waves', 'warm sun' ] 

…without extra empty tokens or extra white space at the start or end of any token.

Edit: It’s been pointed out that extra empty tokens are unavoidable with String.split(), so that’s been removed as a criterion for the ‘perfect’ regex.


Thanks everyone for your responses! I’ve tried to make sure I upvoted everyone who contributed a workable regex that wasn’t essentially a duplicate. Dan’s answer was the most robust (it even handles ‘,white sand, tall waves,and warm sun and ‘ reasonably, with that odd comma placement after the word ‘waves’), so I’ve marked his as the accepted answer. The regex provided by nsayer was a close second.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:23:15+00:00Added an answer on May 10, 2026 at 3:23 pm

    This should be pretty resilient, and handle stuff like delimiters at the end of the string (‘foo and bar and ‘, for example)

    \s*(?:\band\b|,)\s* 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following code: foreach(string str in someObj.GetMyStrings()) { // do some
Suppose I have the following types of data: class Customer { String id; //
Suppose I have the following: struct Person { std::string mName; Birthday mBirthday; }; using
Suppose I have the following: public class MyObject { public string Name {get; set;}
suppose i have the following: boost::unordered_map< string , someValueType > map; someValueType& value =
Suppose I have the following class: public class Test{ public string Length { get;
For example suppose I have the following string we went to the (big) zoo
Suppose I have the following list: var strings = new List<string>(); strings.Add(1); strings.Add(12.456); strings.Add(Foobar);
Suppose I have some string, and run the following tests on it: response.indexOf(</p:panelGrid>); response.matches(.*</p:panelGrid>.*);
Suppose I have an array of string like following: str_arr=[10$, 10$ I am a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.