正则表达式:先用分隔符然后再用另一个分隔符

编程入门 行业动态 更新时间:2024-10-25 04:22:55
本文介绍了正则表达式:先用分隔符然后再用另一个分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所需的行为存在问题.

假设有一个

sourceString = @"name1$$value1^name2$$value2^name3$$value3";

也许是更长的字符串...

maybe more long string...

我想先用 ^ 分隔符分隔,然后再用另一个 $$ 分隔,以基于此名称/值对创建字典.

I'd like to first split by ^ separator and then by another $$ to create dictionary based on this name-value pairs.

此字符串存储在文件中,因此可能太长,任何拆分操作都可能花费太多时间.我希望有一个正则表达式,匹配项由 ^ 匹配,内部组匹配由 $$ 匹配.

This string is stored in file so may be too long, any split operations may take too much time. I hope there is a regex with match by ^ and internal groupmatch by $$.

推荐答案

此正则表达式(.*?)\ $ \ $(.*?)(?:\ ^ | $)将匹配名称值对,这是一个为证明这一点而使用的.要使用它,您可以使用以下代码:

This regex (.*?)\$\$(.*?)(?:\^|$) will match the name value pairs, and here is a Rubular to prove it. And to use it you can use the following code:

var input = "name1$$value1^name2$$value2^name3$$value3"; var pattern = @"(.*?)\$\$(.*?)(?:\^|$)"; var hash = new Dictionary<string, string>(); var match = Regex.Match(input, pattern); while (match.Success) { hash.Add(match.Groups[1].Value, match.Groups[2].Value); match = match.NextMatch(); }

更多推荐

正则表达式:先用分隔符然后再用另一个分隔符

本文发布于:2023-10-16 10:23:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分隔符   再用   先用   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!