c# - c# 正则表达式澄清
问题描述
我无法理解以下代码输出的原因
public static void Main()
{
var p1 = "txtbox";
CheckMatch(p1);
p1 = "txtbox_asd";
CheckMatch(p1);
p1 = "txtbox_asdf";
CheckMatch(p1);
p1 = "txtbox_asd2";
CheckMatch(p1);
}
public static void CheckMatch(string p1)
{
var reg = new Regex(@"txtbox");
if (!reg .IsMatch(p1))
{
Console.WriteLine($"{p1} doesn't match");
}
else
{
Console.WriteLine($"{p1} matches");
}
}
所有案例都返回“匹配”,而我只希望第一个案例匹配。有没有办法让它只有第一个案例匹配?
解决方案
推荐阅读
- python - 为 Pandas DataFrame 中的特定行组合行索引和行值(字符串)
- java - 泛型返回类型方法中的不兼容类型 (Java)
- ios - 即使设置了 CGRect,UIScrollView 中的 UIPageViewController 仍会显示为全屏 - 用户滚动后立即返回正确的帧值
- css - w3.css 从固定页脚下拉
- java - 使用 DSL KStream 到 KTable 转换的 Kafka Tombstoning
- excel - 如何在For循环中计算重复的元素
- javascript - Javascript:启动两个连续等待会产生错误的结果
- parsing - 调试 APL 代码:如何同时使用 `@`(index) 和 `⊢` (right tack)?
- vba - PowerPoint 如何同时运行宏和超链接?
- python - Python在JSON中查找包含在单词/字符串中的单引号