Seetharama
Seetharama

Reputation: 149

String splitting based on Regular Expressions

I am trying to split a string which looks like this:

1.2.1Title of the Chapter

(There is no space between '1' and 'T')

I want the output as:

String 1: 1.2.1
String 2: Title of the chapter

I tried this:

strParagraphs = 1.2.1Title of the chapter;
string[] lines1 = Regex.Split(strParagraphs, "(\\d{1}).(\\d{1}).(\\d{1})");
also
string[] lines1 = Regex.Split(strParagraphs, "^\\w+");

I could not arrive at my desired output. Can someone please suggest, where is it that I am going wrong?

Many thanks

Upvotes: 0

Views: 110

Answers (1)

Maciej Los
Maciej Los

Reputation: 8591

This should work like a charm:

string[] lines = Regex.Split(inputstring, @"(\d+.\d+.\d+)");

Result:

1.2.1 
Title of the Chapter 

Note: the result may differ depending on .net version!

For further information, please see:
Regex.Split Method (String, String)

Upvotes: 1

Related Questions