0

I want to use regex to pull out a string including the beginning and end values.

For example:

Regex regex = new Regex("A Bylaw(.*?)" + @"\.");
var v = regex.Match("blah blah blah A Bylaw to provide for the government and management of the Water Works System in the City and the collection of Rents, Rates, and charges for water. blah blah blah");

This returns:

to provide for the government and management of the Water Works System in the City and the collection of Rents, Rates, and charges for water

I'm looking for:

A Bylaw to provide for the government and management of the Water Works System in the City and the collection of Rents, Rates, and charges for water.

2
  • 1
    Do you want to achieve what you describe, or to achieve it with regexes ? Cause you can probably achieve the same results with usual string methods... Commented Sep 26, 2013 at 16:55
  • If your question has been answered please mark as correct answer Commented Dec 15, 2016 at 10:45

3 Answers 3

1

just change your regex to include them

Regex regex = new Regex(@"A (Bylaw.+?\.)");

http://rubular.com/r/4GRUECzqfx

Sign up to request clarification or add additional context in comments.

Comments

0

Regex regex = new Regex("(?i)A Bylaw(.*)\.");

This gives you the match result not bothering about the case of "A Bylaw".

If the text which you pass has "A BYLAW", then this works perfectly..

@Sam I am , appreciate for the link which you posted.

Comments

0

As I understand, you try to extract the phrases that contains a keyword from a text, to do that, you can use : var regex = new Regex(@"(\.|^)?([^.]*Bylaw.*?(\.|$))"); https://regex101.com/r/KjxWWC/1

  • (\.|^)? will match a dot or the beginning of the text, useful for the first phrase
  • [^.]* will match anything that is not a dot
  • Bylaw is what you are searching
  • .*? will match anything until...
  • (\.|$) will match the closing dot of the phrase

Note that if you use it on a text var matches = regex.Matches("...") the result will be in matches[n].Groups[1].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.