2

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.

1
  • What are exactly the three sections you are after with this example? Commented Mar 10, 2018 at 13:04

2 Answers 2

2

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"]

Code demo

Sign up to request clarification or add additional context in comments.

Comments

0

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 group

Demo C#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.