用于匹配句子中的单词的正则表达式

时间:2021-09-20 04:40:45

I am looking for a C# regex to match a complete word within a sentence.

我正在寻找一个C#正则表达式来匹配句子中的完整单词。

my sentence and pattern looks like this below.

我的句子和模式如下所示。

 string mySentence = "There is no gain in this world without pain";

 string mypattern = string.Format(@"(?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s)){0} (?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))", "pain");

 MatchCollection matches = Regex.Matches(mySentence, mypattern);

I do not get any match using the above pattern.

我没有使用上述模式得到任何匹配。

But if I use this pattern I get a match.

但如果我使用这种模式,我会得到一个匹配。

string mypattern = string.Format(@"\b{0}\b", "patient");

But the problem is it matches hyphen separated word like in-pain as well, where as I am looking for a complete word.

但问题是它匹配连字符分隔的单词,如同痛苦,在哪里,因为我正在寻找一个完整的单词。

Any help appreciated, thanks

任何帮助表示感谢,谢谢

1 个解决方案

#1


2  

Scenario #1 (word(s) not preceded/followed with hyphen)

场景#1(单词之前/之后没有连字符)

Use negative lookarounds (?<!-) (no - before the search phrase) and (?!-) (no - after the search phrase) together with \b:

使用负面外观(?

var mypattern = string.Format(@"(?<!-)\b{0}\b(?!-)", Regex.Escape(search));

Scenario #2 (word(s) enclosed with whitespaces)

场景#2(用空格括起来的字)

I am looking to match a word, which can be followed or preceded by newline or whitespace.

我希望匹配一个单词,可以跟在或前面有换行符或空格。

You need to use lookarounds like this:

你需要使用这样的外观:

var mypattern = string.Format(@"(?<!\S){0}(?!\S)", Regex.Escape(search));

The (?<!\S) lookaround (equal to (?<=\s|^)) requires a whitespace or start of string before the search word. The (?!\S) negative lookahead will require a whitespace symbol after the search word.

(?

The Regex.Escape is a pre-caution step to make sure all special chars inside the search word are treated as literal characters.

Regex.Escape是一个预先警告步骤,以确保搜索词中的所有特殊字符都被视为文字字符。

A note: if your "words" never contain a whitespace, you do not need a regex. Use

注意:如果您的“单词”从不包含空格,则不需要正则表达式。使用

var search = "this";
var sentence = "There is no gain in this world without pain";
var isPresent = sentence.Split().Contains(search);
Console.Write(isPresent); // = > True

#1


2  

Scenario #1 (word(s) not preceded/followed with hyphen)

场景#1(单词之前/之后没有连字符)

Use negative lookarounds (?<!-) (no - before the search phrase) and (?!-) (no - after the search phrase) together with \b:

使用负面外观(?

var mypattern = string.Format(@"(?<!-)\b{0}\b(?!-)", Regex.Escape(search));

Scenario #2 (word(s) enclosed with whitespaces)

场景#2(用空格括起来的字)

I am looking to match a word, which can be followed or preceded by newline or whitespace.

我希望匹配一个单词,可以跟在或前面有换行符或空格。

You need to use lookarounds like this:

你需要使用这样的外观:

var mypattern = string.Format(@"(?<!\S){0}(?!\S)", Regex.Escape(search));

The (?<!\S) lookaround (equal to (?<=\s|^)) requires a whitespace or start of string before the search word. The (?!\S) negative lookahead will require a whitespace symbol after the search word.

(?

The Regex.Escape is a pre-caution step to make sure all special chars inside the search word are treated as literal characters.

Regex.Escape是一个预先警告步骤,以确保搜索词中的所有特殊字符都被视为文字字符。

A note: if your "words" never contain a whitespace, you do not need a regex. Use

注意:如果您的“单词”从不包含空格,则不需要正则表达式。使用

var search = "this";
var sentence = "There is no gain in this world without pain";
var isPresent = sentence.Split().Contains(search);
Console.Write(isPresent); // = > True