Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to find a word before and after a specific word

Tags:

c#

regex

I need a regex that gives me the word before and after a specific word, included the search word itself.

Like: "This is some dummy text to find a word" should give me a string of "dummy text to" when text is my search word.

Another question, it's possible that the string provided will contain more then once the search word so I must be able to retrieve all matches in that string with C#.

Like "This is some dummy text to find a word in a string full with text and words" Should return:

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

EDIT: Actually I should have all the matches returned that contain the search word. A few examples: Text is too read. -> Text is

Read my text. -> my text

This is a text-field example -> a text-field example

like image 775
PitAttack76 Avatar asked Apr 29 '11 13:04

PitAttack76


People also ask

How do you search for a word in regex?

To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.

What does \b mean in regex?

The word boundary \b matches positions where one side is a word character (usually a letter, digit or underscore—but see below for variations across engines) and the other side is not a word character (for instance, it may be the beginning of the string or a space character).

How do you match a specific sentence in regex?

Example is: string pattern = @"(Band) (? <Band>[A-Za-z ]+) (? <City>@[A-Za-z ]+) (?

What does \\ mean in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


2 Answers

//I prefer this style for readability

string pattern = @"(?<before>\w+) text (?<after>\w+)";
string input = "larry text bob fred text ginger fred text barney";
MatchCollection matches = Regex.Matches(input, pattern);

for (int i = 0; i < matches.Count; i++)
{
    Console.WriteLine("before:" + matches[i].Groups["before"].ToString());
    Console.WriteLine("after:" + matches[i].Groups["after"].ToString());
} 

/* Output:
before:larry
after:bob
before:fred
after:ginger
before:fred
after:barney
*/
like image 167
Trey Carroll Avatar answered Oct 12 '22 01:10

Trey Carroll


EDIT:

If you want to grab all the content from the space before first word to the space after the word use:

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

A simple tests:

string input = @"
    This is some dummy text to find a word in a string full with text and words
    Text is too read
    Read my text.
    This is a text-field example
    this is some dummy [email protected] to read";

var matches = Regex.Matches(
    input,
    @"(?:\S+\s)?\S*text\S*(?:\s\S+)?",
    RegexOptions.IgnoreCase
);

the matches are:

dummy text to
with text and
Text is
my text.
a text-field example
dummy [email protected] to
like image 33
Oleks Avatar answered Oct 12 '22 00:10

Oleks