简体   繁体   中英

Split String by logicals with Regex

I've the following string:

string text = "Hello && my || Name & is | Tom"

Now I want to split the string into different parts without the logical operators between the words. I've tried the following, but I get only one string with the whole text.

String[] result= Regex.Split(text, @"\&&\||\&\|");

Whats wrong?

The expected output is an array with 5 strings:

  • Hello
  • my
  • Name
  • is
  • Tom

没有正则表达式解决方案,只是分裂:

String[] result = text.Split(new Char[] { '|', '&' }, StringSplitOptions.RemoveEmptyEntries);

Change your code to,

String[] result= Regex.Split(text, @"\s*[|&]+\s*");

This splits your input according to one or more | or & symbols. \\s* matches zero or more spaces , and [|&]+ matches one or more | or & symbols.

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