我如何 return 一个字符串使用正则表达式从要匹配的字符串中排除一部分
How do i return a string using regex to exclude a part from string to be matched
我正在尝试将字符串列表与正则表达式匹配,如下所示 -
regex.IsMatch(displayName)
但是我检查的第二步需要确保如果 displayName
中的括号内包含任何文本,它不应该 return 匹配。
例如。
if regex
正在寻找包含 "Muds"
的字符串
所以如果我的第一个字符串是
"My name is Muds" -- 它应该 return 匹配(真)。
"My name is ( Muds contained ) " -- 它应该 return NO 匹配(假)。
"My name is ( { Muds } ) " -- 它应该 return NO 匹配(假)。
所以本质上,上面的字符串被简化为"My name is"并且括号内的内容被排除在外。
所以 IsMatch 语句然后减少到 -
regex.IsMatch("My name is")
我制作了一个正则表达式来查找括号内的内容
Regex excludingRegex = "\s?(?:\(.+\)|\[.+\]|<.+>|{.+})\s?"
让上面调用为excluding regex
所以我想做类似
的事情
regex.IsMatch(ExcludingRegex.Replace(displayName,""))
简而言之,我想排除括号内的内容以与第一个正则表达式匹配
我看过很多关于 Negative Lookahead 和从正则表达式中排除上下文的例子,但我无法正确处理我的情况。
任何指点都会很棒。
这很简单 - 使用前瞻:
^(?!.*\([^()]*\)).+
分解后,这表示:
^ # start of the string
(?!.*\([^()]*\)) # neg. lookahead making sure there's nothing between ( and )
.+ # match the line
您好,这是我在控制台应用程序中创建的代码。在这里,我使用函数 regex.split 使用正则表达式拆分字符串(reg exp 检查 {} 中的字符串在哪里)并检查数组中的偶数项我可以检查是否找到字符串 mud 并显示它被发现的地方。赔率中的项目是在已检查的字符串中的 {} 内找到的项目。
您可以下载代码here
static void Main(string[] args)
{
string input = "My name is ( { Muds } ) My name is MUDS ( { Juan } ) My name is ( { Ortiz Mud } ) otra cosa por aca {juan} pero pues aqui mud {yani}";
string pattern = @"\{(.*?)\}";
string[] substrings = Regex.Split(input, pattern);
for(int i= 0; i <= substrings.Length; i ++)
{
if(i % 2 == 0)
{
if (substrings[i].ToLower().Contains("mud"))
{
Console.Write(string.Format("This string has mud outside of brackets. Found in string: {0}", substrings[i].ToString()));
Console.Write("\n");
}
}
}
Console.Read();
}
我正在尝试将字符串列表与正则表达式匹配,如下所示 -
regex.IsMatch(displayName)
但是我检查的第二步需要确保如果 displayName
中的括号内包含任何文本,它不应该 return 匹配。
例如。
if regex
正在寻找包含 "Muds"
所以如果我的第一个字符串是
"My name is Muds" -- 它应该 return 匹配(真)。
"My name is ( Muds contained ) " -- 它应该 return NO 匹配(假)。
"My name is ( { Muds } ) " -- 它应该 return NO 匹配(假)。
所以本质上,上面的字符串被简化为"My name is"并且括号内的内容被排除在外。 所以 IsMatch 语句然后减少到 -
regex.IsMatch("My name is")
我制作了一个正则表达式来查找括号内的内容
Regex excludingRegex = "\s?(?:\(.+\)|\[.+\]|<.+>|{.+})\s?"
让上面调用为excluding regex
所以我想做类似
的事情regex.IsMatch(ExcludingRegex.Replace(displayName,""))
简而言之,我想排除括号内的内容以与第一个正则表达式匹配
我看过很多关于 Negative Lookahead 和从正则表达式中排除上下文的例子,但我无法正确处理我的情况。
任何指点都会很棒。
这很简单 - 使用前瞻:
^(?!.*\([^()]*\)).+
分解后,这表示:
^ # start of the string
(?!.*\([^()]*\)) # neg. lookahead making sure there's nothing between ( and )
.+ # match the line
您好,这是我在控制台应用程序中创建的代码。在这里,我使用函数 regex.split 使用正则表达式拆分字符串(reg exp 检查 {} 中的字符串在哪里)并检查数组中的偶数项我可以检查是否找到字符串 mud 并显示它被发现的地方。赔率中的项目是在已检查的字符串中的 {} 内找到的项目。
您可以下载代码here
static void Main(string[] args)
{
string input = "My name is ( { Muds } ) My name is MUDS ( { Juan } ) My name is ( { Ortiz Mud } ) otra cosa por aca {juan} pero pues aqui mud {yani}";
string pattern = @"\{(.*?)\}";
string[] substrings = Regex.Split(input, pattern);
for(int i= 0; i <= substrings.Length; i ++)
{
if(i % 2 == 0)
{
if (substrings[i].ToLower().Contains("mud"))
{
Console.Write(string.Format("This string has mud outside of brackets. Found in string: {0}", substrings[i].ToString()));
Console.Write("\n");
}
}
}
Console.Read();
}