I'm trying to match this expression:
我试着匹配这个表达式
^COMA1TA2000,.*$
with this text:
这段文字:
# Qualquer linha iniciada por # será ignorada
# Caracteres que não podem serem usados na nomenclatura das copiadoras ou modelos.
# & < > " '
COMA1TA2000,ta-2000,hd,COMB1
#COMA2TA2000,ta-2000,hd,COMB2
#COMA3TA2000,ta-2000,hd,COMB3
I can do that using Notepad++, but I can't with the C# Regex class.
我可以使用Notepad++,但不能使用c# Regex类。
content = sr.ReadToEnd();
string pattern = "^COMA1TA2000,.*$";
if(Regex.IsMatch(content, pattern))
System.Windows.Forms.MessageBox.Show("Test");
What am I missing?
我缺少什么?
3 个解决方案
#1
22
You can use RegexOptions.Multiline
, like so:
您可以使用RegexOptions。多行,如下所示:
Regex.IsMatch(content, pattern, RegexOptions.Multiline)
Docs: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx
文档:http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx
If RegexOptions.Multiline
is not set, ^
and $
will match beginning and the end* of the string, not the line like intended.
如果RegexOptions。没有设置多行,^和$匹配字符串的开始和结束*,不像的目的。
#2
11
Or set multiline option in regex (?m)
: (?m)^COMA1TA2000,.*$
或设置多行选项regex(?):(?)^ COMA1TA2000,。*美元
#3
0
Use File.ReadLines
instead of ReadToEnd
method and apply regex on each line.
使用文件。用readline代替ReadToEnd方法,并在每行上应用regex。
What you're doing now, is reading the entire text as a block of text and MultiLine option sometimes breaks things down (as there are different CRLF characters in different operating systems, and stuff like that).
您现在正在做的是,将整个文本作为文本块读取,而多行选项有时会将事情分解(因为在不同的操作系统中有不同的CRLF字符,诸如此类)。
My suggestion is this:
我的建议是这样的:
string[] lines = File.ReadAllLines("path to your text file");
Regex regex = new Regex("^pattern$");
foreach (string line in lines)
{
Match match = regex.Match(line.Trim())
if (match.Successful)
{
// have your match here.
}
}
#1
22
You can use RegexOptions.Multiline
, like so:
您可以使用RegexOptions。多行,如下所示:
Regex.IsMatch(content, pattern, RegexOptions.Multiline)
Docs: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx
文档:http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx
If RegexOptions.Multiline
is not set, ^
and $
will match beginning and the end* of the string, not the line like intended.
如果RegexOptions。没有设置多行,^和$匹配字符串的开始和结束*,不像的目的。
#2
11
Or set multiline option in regex (?m)
: (?m)^COMA1TA2000,.*$
或设置多行选项regex(?):(?)^ COMA1TA2000,。*美元
#3
0
Use File.ReadLines
instead of ReadToEnd
method and apply regex on each line.
使用文件。用readline代替ReadToEnd方法,并在每行上应用regex。
What you're doing now, is reading the entire text as a block of text and MultiLine option sometimes breaks things down (as there are different CRLF characters in different operating systems, and stuff like that).
您现在正在做的是,将整个文本作为文本块读取,而多行选项有时会将事情分解(因为在不同的操作系统中有不同的CRLF字符,诸如此类)。
My suggestion is this:
我的建议是这样的:
string[] lines = File.ReadAllLines("path to your text file");
Regex regex = new Regex("^pattern$");
foreach (string line in lines)
{
Match match = regex.Match(line.Trim())
if (match.Successful)
{
// have your match here.
}
}