c# - regex to find a word before and after a specific word -


i need regex gives me word before , after specific word, included search word itself.

like: "this dummy text find word" should give me string of "dummy text to" when text search word.

another question, it's possible string provided contain more once search word must able retrieve matches in string c#.

like "this dummy text find word in string full text , words" should return:

  • "dummy text to"
  • "with text and"

i hope 1 can me. thx!

edit: was't complete. should have matches returned contain search word. few examples: text read. -> text is

read text. -> text

this text-field example -> text-field example

i hope i'm more clear now. sry didn't explained start.

thx help!

edit:

if want grab content space before first word space after word use:

(?:\s+\s)?\s*text\s*(?:\s\s+)? 

a simple tests:

string input = @"     dummy text find word in string full text , words     text read     read text.     text-field example     dummy la@text.be read";  var matches = regex.matches(     input,     @"(?:\s+\s)?\s*text\s*(?:\s\s+)?",     regexoptions.ignorecase ); 

the matches are:

dummy text text , text text. text-field example dummy la@text.be to

Comments