简体   繁体   English

正则表达式以匹配管道(|)或行尾

[英]Regex to match pipe (|) or end of line

I have the following regex to match name-value pairs: 我有以下正则表达式匹配名称-值对:

^(?<NameValue>(?<Name>[A-Z][\w]*):(?<Value>[\w]*)(?=(\||\z)))+$

I trying to match the pipe character or end of line. 我试图匹配竖线字符或行尾。 Essentially it should match text like 本质上,它应该与文本匹配

Setting1:Value1|Setting2:Value2|Setting3:Value3

Anyone have any idea what I'm doing wrong. 任何人都知道我在做什么错。 It's been a while since I've had regex stump me like this :) Update: Ended up with 自从我进行了正则表达式以来,已经有一段时间了:)更新:最终

^(?:(?<NameValue>(?<Name>[A-Z][\w]*):(?<Value>[\w]*))(?m:\||$))+

Thanks everyone! 感谢大家!

Regex to match pipe or end of line: 正则表达式以匹配管道或行尾:

(?m:\||$)

The m ultiline flag makes $ match end of line. m ultiline标志使$行的比赛结束。

\\z will only match end of string, never end of line. \\z仅匹配字符串的末尾,而不匹配行尾。

You could use an expression like: 您可以使用类似以下的表达式:

var re = @"(?xm)
    \A
    (?:
       (?<Name>[A-Z]\w*)
       :
       (?<Value>\w*)
       (?: \| | $ )
    )+
    \z";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM