Skip to main content
 首页 » 编程设计

c#之我的正则表达式(简单)有什么问题

2024年01月29日23qlqwjy

我正在尝试制作一个正则表达式来匹配所有出现在一行开头并以 # 开头的单词。

例如:

#region #like 
#hey 

它将匹配#region 和#hey。

这是我现在拥有的:

^#\w* 

对于发布这个问题,我深表歉意。我确定它有一个非常简单的答案,但我一直找不到它。我承认我是一个正则表达式菜鸟。

请您参考如下方法:

你得到的应该可以工作,这取决于你为 RegexOptions 传递的标志.您需要确保通过 RegexOptions.Multiline:

var matches = Regex.Matches(input, @"^#\w*", RegexOptions.Multiline); 

请参阅我上面链接的文档:

Multiline Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.