Reputation: 643
I need a regular expression to parse 3 sections out of the following strings:
"Hello World 12 7" -> I want ["Hello World", "12", "7"]
"Goodbye -4 72" -> I want ["Goodbye", "-4", "72"]
The text part can have any number of words and both numbers can be negative.
Many thanks in advance.
Upvotes: 2
Views: 3366
Reputation: 163297
As an alternative you could match the values:
(?:(?:[A-Z][a-z]+ )*(?:[A-Z][a-z]+)|-?\d+)
That would match:
(?:
Non capturing group
(?:
Non capturing group
[A-Z][a-z]+
Uppercase character followed by one or more lowercase characters and a whitespace)*
Close group and repeat zero or more times(?:
Non capturing group
[A-Z][a-z]+
Uppercase character followed by one or more lowercase characters)
Close group|
Or-?\d+
Optional dash followed by one or more digits)
Close groupUpvotes: 0
Reputation: 3405
You can use method Regex.Split()
and regex (?i) (?![a-z])
.
Details:
(?i)
Case insensitive(?!)
Negative lookahead, match space not followed by [a-z]
C# code:
string[] substrings = Regex.Split(input, "(?i) (?![a-z])");
Output:
["Hello World", "12", "7"]
["Goodbye", "-4", "72"]
Upvotes: 2