如何用正则表达式解析子字符串?

发布于 2024-11-08 03:43:21 字数 639 浏览 3 评论 0原文

我的示例未解析数据是

"8$154#3$021308831#7$NAME SURNAME#11$2166220160#10$5383237309#52$05408166#"

我想解析 $ 和 # 字符串之间的数据。 我想看到这样的结果;

8$# 之间 ->我的数据是 154
3$# 之间 ->我的数据是021308831
7$# 之间 ->我的数据是NAME SURNAME
11$# 之间 ->我的数据是 2166220160
10$# 之间 ->我的数据是 5383237309
52$# 之间 ->我的数据是05408166

感谢您的回复。

My example non-parsed data is

"8$154#3$021308831#7$NAME SURNAME#11$2166220160#10$5383237309#52$05408166#"

I want to parse data that is between $ and # strings.
I want to see result like that;

Between 8$ and # -> My data is 154,
Between 3$ and # -> My data is 021308831,
Between 7$ and # -> My data is NAME SURNAME,
Between 11$ and # -> My data is 2166220160,
Between 10$ and # -> My data is 5383237309,
Between 52$ and # -> My data is 05408166.

Thanks for your reply.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冬天旳寂寞 2024-11-15 03:43:21
(\d+\$)(.*?)#

在 Rubular 上查看

您会找到第一部分(例如 8$ )在捕获组 1 中,相应的数据在组 2 中。

括号负责将结果存储在这些捕获组中。 \d+ 将匹配至少一位数字。 .*? 是对下一个 # 之前的所有内容的惰性匹配。

(\d+\$)(.*?)#

See it on Rubular

You will find the first part (e.g. 8$) in the capturing group 1 and the according data in the group 2.

The brackets are responsible, that the result is sotred in those capturing groups. The \d+ will match at least one digit. The .*? is a lazy match for everything till the next #.

瞄了个咪的 2024-11-15 03:43:21
class Program
{
    static void Main(string[] args)
    {
        string text = "8$154#3$021308831#7$NAME SURNAME#11$2166220160#10$5383237309#52$05408166#";
        string[] values = text.Split('
, '#');
        for (var i = 0; i < values.Length - 1; i = i + 2)
        {
            Console.WriteLine("Between " + values[i] + "$ and # -> My data is " + values[i+1]);
        }
        Console.ReadLine();
    }
}
class Program
{
    static void Main(string[] args)
    {
        string text = "8$154#3$021308831#7$NAME SURNAME#11$2166220160#10$5383237309#52$05408166#";
        string[] values = text.Split('
, '#');
        for (var i = 0; i < values.Length - 1; i = i + 2)
        {
            Console.WriteLine("Between " + values[i] + "$ and # -> My data is " + values[i+1]);
        }
        Console.ReadLine();
    }
}
灯角 2024-11-15 03:43:21

可以根据#拆分成数组。
这样

String[] entries = data.Split('#');

你就会得到一个包含“8$154”、“3$021308831”等的数组。

现在你只需处理这些条目并在美元符号处分割每个条目:

String[] tmp = entries[0].Split('

所以你会得到

tmp[0] = "8";
tmp[1] = "154";

一些支票,你会很高兴。我想这里不需要正则表达式。

如果你有“8$15$4#3$021308831”,那么你将进入tmp

tmp[0] = "8"; // your key!
tmp[1] = "15"; // data part
tmp[2] = "4"; // data part ($ is missing!)

所以你必须连接索引1以上的所有tmp:

StringBuilder value = new StringBuilder();
for(int i = 1; i < tmp.Length; i++)
{
    if(i > 1) value.Append("$");
    value.Append(tmp[i]);
}
);

所以你会得到

一些支票,你会很高兴。我想这里不需要正则表达式。

如果你有“8$15$4#3$021308831”,那么你将进入tmp

所以你必须连接索引1以上的所有tmp:

You can split into array based on #.
With

String[] entries = data.Split('#');

you will get an arrays with "8$154", "3$021308831", etc.

Now you just work with the entries and split each one at the dollar sign:

String[] tmp = entries[0].Split('

So you get

tmp[0] = "8";
tmp[1] = "154";

Build in some checks and you will be happy. No need for regex here I suppose.

If you have "8$15$4#3$021308831" then you will get in tmp:

tmp[0] = "8"; // your key!
tmp[1] = "15"; // data part
tmp[2] = "4"; // data part ($ is missing!)

So you would have to concat all tmp above index 1:

StringBuilder value = new StringBuilder();
for(int i = 1; i < tmp.Length; i++)
{
    if(i > 1) value.Append("$");
    value.Append(tmp[i]);
}
);

So you get

Build in some checks and you will be happy. No need for regex here I suppose.

If you have "8$15$4#3$021308831" then you will get in tmp:

So you would have to concat all tmp above index 1:

绻影浮沉 2024-11-15 03:43:21

好的,采用 stema 的表达式,有效。

using System.Text.RegularExpressions;

string nonParsed = "8$...";

MatchCollection matches = Regex.Matches(nonparsed, @"(\d+\$)(.*?)#");

StringBuilder result = new StringBuilder();

for(int i = 0; i < matches.Count; i++)
{
    Match match = matches[i];

    result.AppendFormat("Between {0} and #-> My data is {1}")
        match.Groups[1].Value,
        match.Groups[2].Value);

    if (i < matches.Count - 1)
    {
        result.AppendLine(",");
    }
    else
    {
        result.Append(".");
    }
}

return result.ToString();

感谢 stema,这可以解决值中重复的 $ 问题。

Ok, taking stema's expression, which works.

using System.Text.RegularExpressions;

string nonParsed = "8$...";

MatchCollection matches = Regex.Matches(nonparsed, @"(\d+\$)(.*?)#");

StringBuilder result = new StringBuilder();

for(int i = 0; i < matches.Count; i++)
{
    Match match = matches[i];

    result.AppendFormat("Between {0} and #-> My data is {1}")
        match.Groups[1].Value,
        match.Groups[2].Value);

    if (i < matches.Count - 1)
    {
        result.AppendLine(",");
    }
    else
    {
        result.Append(".");
    }
}

return result.ToString();

Thanks to stema, this copes with the $ repeating within the value.

云朵有点甜 2024-11-15 03:43:21

如果你想使用正则表达式,这应该可以做到。

\$([\w\d\s]+)\#

If you want to use regex this should do it.

\$([\w\d\s]+)\#
甩你一脸翔 2024-11-15 03:43:21

这将匹配 $ 和 # 之间:

\$(.*?)#

This will match betweel $ and #:

\$(.*?)#
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文