正则表达式来重复跨越CDL捕获? [英] Regex to repeat a capture across a CDL?

查看:155
本文介绍了正则表达式来重复跨越CDL捕获?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种形式的一些数据:

I have some data in this form:

@"Managers Alice, Bob, Charlie
Supervisors Don, Edward, Francis"

我需要一个平坦的输出是这样的:

I need a flat output like this:

@"Managers Alice
Managers Bob
Managers Charlie
Supervisors Don
Supervisors Edward
Supervisors Francis"

实际的职称上面可能是任何一个词,没有独立列表,从工作。

The actual "job title" above could be any single word, there's no discrete list to work from.

更换 \ r \ñ是很容易的,因为是第一个替换:

Replacing the with \r\n is easy enough, as is the first replacement:

Replace (^|\r\n)(\S+\s)([^,\r\n]*),\s
With $1$2$3\r\n$2

不过,捕获的其他名字和应用相同的preFIX是什么今天躲避我。有什么建议?

But capturing the other names and applying the same prefix is what is eluding me today. Any suggestions?

我在找一个系列中的一个或多个 RegEx.Replace()仅调用,无需在C#中的任何LINQ或程序code,当然,这将是微不足道的。的实施是不能直接在C#code,我在配置,使用了一系列的.NET正EX pressions转变从一个输入数据的通用分析工具各种来源的多种用途。

I'm looking for a series of one or more RegEx.Replace() calls only, without any LINQ or procedural code in C#, which would of course be trivial. The implementation is not directly in C# code, I'm configuring a generic parsing tool that uses a series of .NET regular expressions to transform incoming data from a variety of sources for several uses.

推荐答案

下面是一个纯替代的解决方案:

Here's a pure-Replace solution:

string s = @"Managers Alice, Bob, Charlie
Supervisors Don, Edward, Francis";
Regex r = new Regex(@"(?:^\w+)?( \w+)(?<=^(\w+)\b.*)[,\r\n]*",
    RegexOptions.Multiline);
string s1 = r.Replace(s0, "$2$1\r\n");

每名被匹配

在时,后向返回到当前行的开头捕捉标题。该(?:^ \ w +) [,\ r \ n]的* 只存在消耗?部分你不希望保留的字符串。

After each name is matched, the lookbehind goes back to the beginning of the current line to capture the title. The (?:^\w+)? and [,\r\n]* are only there to consume the parts of the string you don't want to keep.

这篇关于正则表达式来重复跨越CDL捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆