3

我有一个这样的字符串:

|T1| This is some text for the first tag |T2| this is some text for the second tag

我需要解析出标签和与每个标签相关联的文本。这些标签事先不知道,但它们由 . 分隔\|\w+\|

我知道我可以在这里做一些事情来捕获组等等,但是在 powershell 中搞砸之后,我能想到的最好的方法是首先使用\|\w+\|.*ExplicitCapture 选项隔离每个配对,然后解析出标签和文本那里。

但这正在做双倍的工作,而且完全不是超酷的haxor。regex-pro 的方法是什么?

编辑:实际上我意识到已经很晚了,我误读了我的结果。以上实际上不起作用,所以现在我什至没有一个糟糕的解决方案。

4

1 回答 1

4
\|(?<tag>\w+)\|(?<text>[^|]*)

火柴|T1| This is some text for the first tag |T2| this is some text for the second tag

进入

 |T1| This is some text for the first tag 
 |T2| this is some text for the second tag

编辑:使用正则表达式组获得部分匹配;

var tagName = match.Groups["tag"].Value;
var text = match.Groups["text"].Value;

切换到命名组而不是编号

于 2012-04-07T06:30:48.523 回答