Regular expressions: matching an arbitrary word

It is necessary to combine the first part of the sentence, down to a certain word. However, this word is optional, in which case I want to match the whole sentence. For example:

I have a proposal with a proposal that I do not want.

I have a suggestion and I like it.

In the first case, I want "I have a sentence" . In the second case, I want "I have a sentence and I like it."

Lookarounds will give me the first case, but as soon as I try to make it optional to cover the second case, I get the first sentence. I tried to make the expression lazy ... without a bone.

Code that works for the first case:

 var regEx = new Regex(@".*(?=with)"); string matchstr = @"I have a sentence with a clause I don't want"; if (regEx.IsMatch(matchstr)) { Console.WriteLine(regEx.Match(matchstr).Captures[0].Value); Console.WriteLine("Matched!"); } else { Console.WriteLine("Not Matched : ("); } 

The expression I would like to work:

 var regEx = new Regex(@".*(?=with)?"); 


Any suggestions?

Thanks in advance!
James

+6
c # regex
source share
3 answers

There are several ways to do this. You can do something like this:

 ^(.*?)(with|$) 

The first group is reluctant to compare, i.e. as few characters as possible. We have full correspondence if this group is followed by either with or the end of the line $ anchor .

Given this input:

 I have a sentence with a clause I don't want. I have a sentence and I like it. 

Then there are two matches ( as seen on rubular.com ):

  • Match 1:
    • Group 1: "I have a sentence "
    • Group 2: "with"
  • Match 2:
    • Group 1: "I have a sentence and I like it" . "I have a sentence and I like it"
    • Group 2: "" (empty line)

You can do grouped striping without capture with (?:with|$) if you don't need to distinguish between the two cases.

Related Questions

  • The difference between .*? and .* for regular expression
+11
source share

If I understand your need correctly, do you want to match either the sentence before the word "c", or if it does not match, does it all correspond? Why not write a regular expression to explicitly search for two cases?

 /(.*) with |(.*)/ 

Would it have both cases?

+1
source share
 string optional = "with a clause I don't want" string rx = "^(.*?)" + Regex.Escape(optional) + ".*$"; // displays "I have a sentence" string foo = "I have a sentence with a clause I don't want."; Console.WriteLine(Regex.Replace(foo, rx, "$1")); // displays "I have a sentence and I like it." string bar = "I have a sentence and I like it."; Console.WriteLine(Regex.Replace(bar, rx, "$1")) 

If you don't need the complex match provided by the regular expression, you can use a combination of IndexOf and Remove . (And, obviously, you could distract the logic to an auxiliary and / or extension method or similar):

 string optional = "with a clause I don't want" // displays "I have a sentence" string foo = "I have a sentence with a clause I don't want."; int idxFoo = foo.IndexOf(optional); Console.WriteLine(idxFoo < 0 ? foo : foo.Remove(idxFoo)); // displays "I have a sentence and I like it." string bar = "I have a sentence and I like it."; int idxBar = bar.IndexOf(optional); Console.WriteLine(idxBar < 0 ? bar : bar.Remove(idxBar)); 
+1
source share

All Articles