Regex repeat capture via CDL?

I have some data in this form:

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

I need a flat output as follows:

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

The actual "job title" above can be any one word, there can be no discrete list from it.

Replacing with is \r\nquite simple, like the first replacement:

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

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

I am only looking for one or more calls RegEx.Replace(), without any LINQ or C # procedural code, which, of course, would be trivial. Implementing not directly in C # code, I am setting up a general parsing tool that uses a number of .NET regular expressions to convert incoming data from different sources for several purposes.

+1
4

:

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");

lookbehind , . (?:^\w+)? [,\r\n]* , , .

+1

, LINQ?

string s = "Managers Alice, Bob, Charlie\r\nSupervisors Don, Edward, Francis";

var result =
    from line in s.Split(new string[] { "\r\n" }, StringSplitOptions.None)
    let parts = line.Split(new char[] { ' ' }, 2)
    let title = parts[0]
    let names = parts[1]
    from name in names.Split(new char[] { ',' })
    select title.Trim() + " " + name.Trim();

string.Join("\r\n", result)

Managers Alice
Managers Bob
Managers Charlie
Supervisors Don
Supervisors Edward
Supervisors Francis
0

, , .

string input = @"Managers Alice, Bob, Charlie
Supervisors Don, Edward, Francis";
string pattern = @"(?<Title>\w+)\s+(?:(?<Names>\w+)(?:,\s+)?)+";

foreach (Match m in Regex.Matches(input, pattern))
{
    Console.WriteLine("Title: {0}", m.Groups["Title"].Value);
    foreach (Capture c in m.Groups["Names"].Captures)
    {
        Console.WriteLine(c.Value);
    }

    Console.WriteLine();
}

, "" . . , , , .

: (?<Title>\w+)\s+(?:(?<Names>\w+)(?:,\s+)?)+

  • (?<Title>\w+)\s+ - Title. .
  • (?: (?\w +) (?:,\s +)?) + - Names (?<Names>\w+), ( , (?:...)) (?:,\s+)?, , ?. , , ​​ (?:...)+, , .
0

^(\w+)[ \t]+(\w+),[ \t]+(.+)$

\1 \2\r\n\1 \3

You need to apply it twice to your example, three times, if the list of managers grows to four, etc.

So in C #:

resultString = Regex.Replace(subjectString, @"^(\w+)[ \t]+(\w+),[ \t]+(.+)$", @"$1 $2\r\n$1 $3", RegexOptions.Multiline);

Explanation:

^: match the beginning of the line

(\w+)[ \t]+: match any number of alnum characters, fix the match; space matching

(\w+): match the next word, then

,[ \t]+(.+)$correspond to a comma, spaces, and then everything that follows to the end of the line. This will only match if the string still contains the content to be split.

0
source

All Articles