如何匹配或开始使用C#的正则表达式一行结束了吗? [英] How to match begin or end of a line using C#'s Regex?

查看:147
本文介绍了如何匹配或开始使用C#的正则表达式一行结束了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配这个EX pression:

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

我能做到这一点使用的Notepad ++ 的,但我不能用C#<一href="https://msdn.microsoft.com/en-us/library/system.text.regularex$p$pssions.regex%28v=vs.110%29.aspx"相对=nofollow>正则表达式类。

I can do that using Notepad++, but I can't with the C# Regex class.

content = sr.ReadToEnd();
string pattern =  "^COMA1TA2000,.*$";
if(Regex.IsMatch(content, pattern))
System.Windows.Forms.MessageBox.Show("Test");

我在想什么?

What am I missing?

推荐答案

您可以使用 RegexOptions.Multiline ,像这样:

Regex.IsMatch(content, pattern, RegexOptions.Multiline)

文档:<一href="http://msdn.microsoft.com/en-us/library/system.text.regularex$p$pssions.regexoptions.aspx">http://msdn.microsoft.com/en-us/library/system.text.regularex$p$pssions.regexoptions.aspx

如果 RegexOptions.Multiline 没有设置, ^ $ 将匹配开始和字符串的,不是的的喜欢之意。

If RegexOptions.Multiline is not set, ^ and $ will match beginning and the end* of the string, not the line like intended.

这篇关于如何匹配或开始使用C#的正则表达式一行结束了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆