Regex's match,主题字符串中的破折号
本文关键字:字符串 破折号 match Regex | 更新日期: 2023-09-27 18:08:56
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:我承认我的问题确实模棱两可,所以我的目标是在任何字符串中找到parameter字
要匹配-
之前的任何单词,您可以简单地这样做:
/('w+)'s*'-/
在上面的例子中,第一组是" parameter "。例如,
"Foo - bar baz" // first group will be "Foo"
"Hello - World" // first group will be "Hello"
更新:我似乎误解了你的问题。如果目的只是想知道单词"parameter"是否存在于字符串中,您可以使用
String.Contains
:
"Parametre - Bloc Notes".Contains("Parametre"); // true
或者,如果你关心单词边界(即,你不想要匹配"Parametres"),你仍然可以使用regex:
/'bParametre'b/