Lars
Lars

Reputation: 10573

How to do a regex split on multiple words (not characters)

I'd like to do a regex split on multiple words, not just characters

For example the string:

and(animal = fish)or(food = meat)

Should give me:

1: (animal = fish)
2: (food = meat)

Upvotes: 0

Views: 263

Answers (1)

Lars
Lars

Reputation: 10573

Regex.Split("and(animal = fish)or(food = meat)", @"and|or")

Gives you:

[0]: ""
[1]: "(animal = fish)"
[2]: "(food = meat)"

If you want to retain the delimiters use:

Regex.Split("and(animal = fish)or(food = meat)", @"(and)|(or)")

Gives you:

[0]: ""
[1]: "and"
[2]: "(animal = fish)"
[3]: "or"
[4]: "(food = meat)"

Upvotes: 1

Related Questions