-1

As the title indicates, how can I pass an array of delimiters to a Regex.Split function? I'm needing to split a sentence (string) by a list of words (also strings).

Lloyd
  • 435
  • 3
  • 12
  • 29
  • 1
    Can you post sample input and sample strings to split by? Maybe, also, the code you've tried – dvo Aug 09 '19 at 19:30
  • Possible duplicate of [string.split - by multiple character delimiter](https://stackoverflow.com/questions/1254577/string-split-by-multiple-character-delimiter) – Thomas Weller Aug 09 '19 at 19:31
  • Lookup alternations. – sticky bit Aug 09 '19 at 19:34
  • In `String.Split` you can pass an array of delimiters; however, not in `Regex.Split`. Instead, you pass a regex expression describing a search pattern for split positions. It can be as simple as `string[] parts = Regex.Split(input, "a|b|c");` This would split on the 3 characters "a" to "c". You should probably use `String.Split`. – Olivier Jacot-Descombes Aug 09 '19 at 19:42

1 Answers1

1

You can build a regex pattern from "delimiter" words like this:

var delim = new string[] {"fox", "lazy"};
var pattern =  @"(?:\s|^)(?:" + string.Join("|", delim.Select(Regex.Escape)) + @")(?:\s|$)";

\s and string anchors at the beginning and at the end ensure that delimiters include all white space around them, and that you avoid the Scunthorpe problem. Using Regex.Espace ensures that delimiters with regex meta-characters do not break your code.

The resultant pattern looks as follows:

(?:\s|^)(?:fox|lazy)(?:\s|$)

Demo 1

If you would like to keep delimiter words among the tokens, change regex to use them in a lookahead/lookbehind:

var delimGroup = "(?:"+string.Join("|", delim.Select(Regex.Escape))+")";
var pattern =  @"\s(?="+delimGroup+")|(?<="+delimGroup+@")\s";

Demo 2

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you so much for your help. That seems to be what I'm looking for.. is there any way I can preserver the delimiters in the final result? At the moment the `fox` and `lazy` ar excluded from the final string... – Lloyd Aug 09 '19 at 21:29