如何用net amount来匹配字符串“网和量之间可以有任意数量的空格,包括零)
这两个单词之间的空格可以是任何空格,两个字符串的精确匹配应该存在。但是净额(第一个带空格的字符串)可以是任何字符串的一部分,比如Rate Net Amount或Rate CommissionNet Amount.。
匹配应该是case-insensitive.
发布于 2010-04-02 11:24:38
如果只想检查是否存在匹配,请使用IsMatch
using System;
using System.Text.RegularExpressions;
class Program
{
public static void Main()
{
string s = "Net Amount";
bool isMatch = Regex.IsMatch(s, @"Net\s*Amount",
RegexOptions.IgnoreCase);
Console.WriteLine("isMatch: {0}", isMatch);
}
}更新:在您的注释中,听起来只在运行时才知道要搜索的字符串。您可以尝试动态构建正则表达式,例如:
using System;
using System.Text.RegularExpressions;
class Program
{
public static void Main()
{
string input = "Net Amount";
string needle = "Net Amount";
string regex = Regex.Escape(needle).Replace(@"\ ", @"\s*");
bool isMatch = Regex.IsMatch(input, regex, RegexOptions.IgnoreCase);
Console.WriteLine("isMatch: {0}", isMatch);
}
}发布于 2010-04-02 11:18:29
使用正则表达式。查看System.Text.RegularExpressions名称空间,即Regex类:
var regex = new RegEx("net(\s+)amount", RegexOptions.IgnoreCase);
// ^^^^^^^^^^^^^^^
// pattern参数字符串就是所谓的正则表达式模式。正则表达式模式描述字符串将与之匹配的内容。它们是用专门的语法表示的。谷歌的regular expressions和你应该找到大量的信息关于雷克斯。
用法示例:
bool doesInputMatch = regex.IsMatch("nET AmoUNT");
// ^^^^^^^^^^^^^^^^^
// test input发布于 2010-04-02 11:26:41
您可以使用
Regex.IsMatch(SubjectString, @"net\s*amount", RegexOptions.Singleline | RegexOptions.IgnoreCase);https://stackoverflow.com/questions/2566322
复制相似问题