正则表达式模式匹配串联

发布于 2024-11-28 13:58:34 字数 526 浏览 0 评论 0原文

是否可以仅使用正则表达式语法来连接正则表达式模式匹配的结果?

具体实例是一个程序允许正则表达式语法从文件中提取信息,但我希望它从多个部分中提取并连接结果。

例如:

输入字符串:1234567890

所需结果字符串:2389

正则表达式模式匹配:(?<=1).+(?=4)%%(?<; =7).+(?=0)

其中 %% 表示某种形式的串联语法。使用语法开始和结束很重要,因为我知道字段名称,但不知道字段的值。

是否存在类似于 %% 的关键字?有没有更聪明的方法来做到这一点?是否必须更改代码以允许多个正则表达式输入并自动连接?

同样,要连接的片段可能相距很远,中间有未知字符。所知道的只是子字符串周围的信息。

2011-08-08 编辑:该程序是用 C# 编写的,但与寻找基于正则表达式的解决方案相比,更改代码是一项重大任务。

Is it possible to concatenate the results of Regex Pattern Matching using only Regex syntax?

The specific instance is a program is allowing regex syntax to pull info from a file, but I would like it to pull from several portions and concatenate the results.

For instance:

Input string: 1234567890

Desired result string: 2389

Regex Pattern match: (?<=1).+(?=4)%%(?<=7).+(?=0)

Where %% represents some form of concatenation syntax. Using starting and ending with syntax is important since I know the field names but not the values of the field.

Does a keyword that functions like %% exist? Is there a more clever way to do this? Must the code be changed to allow multiple regex inputs, automatically concatenating?

Again, the pieces to be concatenated may be far apart with unknown characters in between. All that is known is the information surrounding the substrings.

2011-08-08 edit: The program is written in C#, but changing the code is a major undertaking compared to finding a regex-based solution.

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

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

发布评论

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

评论(1

娇柔作态 2024-12-05 13:58:34

如果不确切知道您想要匹配什么以及您使用什么语言,就不可能给您准确的答案。但是,处理此类问题的常用方法是使用分组

在 C# 中:

string pattern = @"(?<=1)(.+)(?=4).+(?<=7)(.+)(?=0)";
Match m = Regex.Match(input, pattern);

string result = m.Groups[0] + m.Groups[1];

同样的方法也可以应用于许多其他语言。

编辑

如果您无法更改代码,则无法完成您想要的任务。原因是在 C# 中,正则表达式字符串本身对输出没有任何影响力。要更改结果,您必须更改 Regex 类的被调用方法或随后执行一些额外的工作。事实上,调用的方法很可能只返回一个 Match 对象或一个匹配对象列表,无论输入的正则表达式字符串如何,这两者都不会执行您想要的操作。

Without knowing exactly what you want to match and what language you're using, it's impossible to give you an exact answer. However, the usual way to approach something like this is to use grouping.

In C#:

string pattern = @"(?<=1)(.+)(?=4).+(?<=7)(.+)(?=0)";
Match m = Regex.Match(input, pattern);

string result = m.Groups[0] + m.Groups[1];

The same approach can be applied to many other languages as well.

Edit

If you are not able to change the code, then there's no way to accomplish what you want. The reason is that in C#, the regex string itself doesn't have any power over the output. To change the result, you'd have to either change the called method of the Regex class or do some additional work afterwards. As it is, the method called most likely just returns either a Match object or a list of matching objects, neither of which will do what you want, regardless of the input regex string.

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