0
var subject = "Parametre - Bloc Notes"
var match = Regex.Match(subject, "(?i)(?:blah|para|foo).*?");
// This will work 
//"Para" doesn't match "Param" and it is before the dash

var subject = "Parametre - Bloc Notes"
var match = Regex.Match(subject, "(?i)(?:blah|blo|foo).*?");
// This will not work
// "Blo" match "Bloc" and it is after the dash

我认为“-”是我误解的主要原因。

编辑:

我真的很抱歉,所以我希望正则表达式在破折号之前匹配 Param,我该怎么做?

编辑2:

我承认我的问题真的很模棱两可,所以我的目标是在任何字符串中找到参数词。

4

1 回答 1

1

要匹配之前出现的任何单词,-您可以简单地执行以下操作:

/(\w+)\s*\-/

在上面的示例中,第一组将是“参数”。例如,

"Foo - bar baz" // first group will be "Foo"

"Hello - World" // first group will be "Hello"

更新:我似乎误解了你的问题。如果目的只是想知道字符串中是否存在“参数”一词,您可以使用String.Contains

"Parametre - Bloc Notes".Contains("Parametre"); // true

或者,如果您关心单词边界(即,您不想匹配“参数”),您仍然可以使用正则表达式:

/\bParametre\b/
于 2013-04-12T13:33:08.997 回答