lotomax
lotomax

Reputation: 113

How can i create this regex about this condition

I want to do this via regex: sample: father goes visit his grandfather and and his father today.

how do i select bold two father via regular expression? thanks a lot.

Upvotes: 1

Views: 62

Answers (3)

Santosh Panda
Santosh Panda

Reputation: 7341

var matchCollection = Regex.Matches("father goes visit his grandfather and and his father today.", "\bfather\b");

foreach (Match m in matchCollection)
{
     // Process your code for each match
}

Upvotes: 1

Ant P
Ant P

Reputation: 25221

I'm not sure why you need regular expressions for this, since you can "select" the string you want simply by doing:

String myStr = "father";

Alternatively, if you need to replace the substring in the string, you can just do the following:

String myStr = "father goes visit his grandfather and and his father today."
myStr = myStr.Replace(" father ", " someOtherString ");

But, if you really want to use RegEx, you can get a MatchCollection by doing:

String myStr = "father goes visit his grandfather and and his father today."
Regex.Mathces(myStr, "\bfather\b");

It's not a very interesting regex because it is simply an unambiguous string literal.

Upvotes: 1

Civa
Civa

Reputation: 2166

try this

var matchs=Regex.matches("father goes visit his grandfather and and his father today.",@"\bfather\b");

Upvotes: 1

Related Questions