我有以下情况
F:(.+?)(?=#)
F:(.+?)(?=\b)
所以我想出了以下两个表达式
Status Event
SUCCESS Run
SUCCESS Walk
SUCCESS Run
FAILED Walk
是否可以将两个表达式组合在一起并为哈希值提供更高的优先级,我应该在寻找正则表达式中的哪个概念?
答案 0 :(得分:2)
您可以使用正则表达式提供的 if-else
条件
^F:(?(?=.*#$).*$|\w*)
正则表达式细分
^ #Start of string
F: #Match F: literally
(? #Conditional regex begin
(?=.*#$) #if-condition. # is in end of string
.*$ #if above is true match entire string
|
\w* #else match word character. You can use .+?\b also
)
<强> Regex Demo 强>
.net code
var pattern = "(?m)^F:(?(?=.*#$).*$|\\w*)";
var input = "F:xxxx xxxx#\nF:xxxx xxxx";
Regex rgx = new Regex(pattern);
Match match = rgx.Match(input);
while (match.Success) {
Console.WriteLine(match.Groups[0].Value.Trim());
match = match.NextMatch();
}
<强> .net fiddle demo 强>