简体   繁体   中英

Splitting string selectively on tokens

I am splitting my string about the word "by" in order to get an array that is

[<title>, <date>], 

where < date > is an optional entry.

So for example, if I have the sentence "get eggs by friday", it would be split into

[get eggs, friday].

However, if the title contains the word "by", it would be split as well, for example "chill by the beach by friday" gives

[chill, the beach, friday] 

which is not what i want.

Furthermore, because my date is optional, "chill by the beach" would give me

[chill, the beach]

I did think about splitting around the last "by" only, but this example proves that this wouldnt work because the last "by" is in the title.

I am currently using Java regex and the command String.split(...) for this. Is there anyway to get what I want or is this impossible without escaping the front "by"("chill \\by the beach by friday" then removing the "\\" after the split)?

The below regex would split the string into two only based on the delimiter by which exists at the last followed by exactly one word.

string.split("\\s+by\\s+(?=\\S+$)");

or

Include all the possible day's inside the lookahead.

string.split("\\s+by\\s+(?=(?:friday|monday|saturday)$)");

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