Burfi
Burfi

Reputation: 229

Regular Expression to split string


I am trying to parse the below string :

*A. BOSCOLO1,4, J. A. STARR2, N. LUNARDI4, C. ORI4, J. P. BENNETT, Jr3, V. JEVTOVIC-TODOROVIC2,3,4,

The output expected is below :

*A. BOSCOLO1,4
J. A. STARR2
N. LUNARDI4
C. ORI4
J. P. BENNETT, Jr3
V. JEVTOVIC-TODOROVIC2,3,4

I can not split them on the delimiter (,) as it's inconsistent . The above are the names followed by one or more digits. Words followed by digit & commas until next word and No names are allowed which do not have any digits.
I have no idea how to write REGEX for the same . Any help would be highly appreciated.
Thanks

Upvotes: 1

Views: 138

Answers (1)

Guffa
Guffa

Reputation: 700800

You can use a regular expression like this:;

[*A-Z]\D+\d+(,\d+)*

Example:

string data = "*A. BOSCOLO1,4, J. A. STARR2, N. LUNARDI4, C. ORI4, J. P. BENNETT, Jr3, V. JEVTOVIC-TODOROVIC2,3,4,";

MatchCollection matches = Regex.Matches(data, @"[*A-Z]\D+\d+(,\d+)*");

foreach (Match x in matches) {
  Console.WriteLine(x);
}

Output:

*A. BOSCOLO1,4
J. A. STARR2
N. LUNARDI4
C. ORI4
J. P. BENNETT, Jr3
V. JEVTOVIC-TODOROVIC2,3,4

Upvotes: 3

Related Questions