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.

4

3 回答 3

1

只需更改您的正则表达式以包含它们

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

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

于 2013-09-26T17:06:52.097 回答
0

正则表达式 regex = new Regex("(?i)A Bylaw(.*)\.");

这为您提供了匹配结果,而不用担心“章程”的情况。

如果您传递的文本有“A BYLAW”,那么这非常有效..

@Sam 我是,感谢您发布的链接。

于 2013-09-26T17:58:50.933 回答
0

据我了解,您尝试从文本中提取包含关键字的短语,为此,您可以使用:var regex = new Regex(@"(\.|^)?([^.]*Bylaw.*?(\.|$))"); https ://regex101.com/r/KjxWWC/1

  • (\.|^)?将匹配一个点或文本的开头,对第一个短语很有用
  • [^.]*将匹配任何不是点的东西
  • Bylaw是您正在搜索的内容
  • .*?将匹配任何东西,直到...
  • (\.|$)将匹配短语的结束点

请注意,如果您在文本上使用它,var matches = regex.Matches("...")结果将在matches[n].Groups[1].

于 2016-11-16T12:26:27.263 回答