Regex 正则表达式组捕获,如何在下一个单词之前停止
我有以下正则表达式:Regex 正则表达式组捕获,如何在下一个单词之前停止,regex,regex-group,Regex,Regex Group,我有以下正则表达式: Defaults(.*)Class=\"(?<class>.*)\"(.*)StorePath=\"(?<storePath>.*)\" 我正在努力实现以下目标: class Class name here storePath Any store path here 但是,我得到的结果是: class Class name here storePath Any store path here SqlTable="SqlTableName" 如何
Defaults(.*)Class=\"(?<class>.*)\"(.*)StorePath=\"(?<storePath>.*)\"
我正在努力实现以下目标:
class Class name here
storePath Any store path here
但是,我得到的结果是:
class Class name here
storePath Any store path here SqlTable="SqlTableName"
如何在Sqltable文本之前停止
语言是C#,正则表达式引擎是内置的.NET框架
非常感谢 由@ahmed abdelhameed提出的解决方案解决了这个问题,我忘记了非贪婪
Defaults(.*)Class=\"(?<class>.*)\"(.*)StorePath=\"(?<storePath>.*?)\"
默认值(.*)类=\“(?.*)\”(.*)存储路径=\“(?.*)”
谢谢 在
storePath
组中,对任何字符进行零次或多次匹配(贪婪匹配)。贪婪匹配的意思是,它将返回尽可能多的字符,因此它将保持匹配字符,直到到达最后出现的“
您需要做的是通过将*
替换为*?
,将贪婪匹配转换为惰性匹配。惰性匹配的意思是,它将返回尽可能少的字符,因此在您的情况下,它将保持匹配字符,直到它第一次出现“
只需将您的正则表达式替换为:
Defaults(.*)Class=\"(?<class>.*)\"(.*)StorePath=\"(?<storePath>.*?)\"
默认值(.*)类=\“(?.*)\”(.*)存储路径=\“(?.*)”
参考资料:
Class="(.+?)".+?StorePath="(.+?)"
这个?就是说比赛不贪心,基本上比赛尽量少。
这将导致它捕获到下一个“将(?*)
更改为(?*)
。
Class="(.+?)".+?StorePath="(.+?)"