How to split a multipart string into two parts

I could do this with basic string manipulation but I was hoping that there was a cleaner method, perhaps using regular expressions. What I want to do is separate a multipart string into two basic parts. Assuming I have the following string:

"First.Second.Third.Fourth"

The result I would want is:

"First.Second.Third"
"Fourth"

Is there a nice clean Regex or Linq way of doing this or am I just going to have to use the regular string.Split() method and manually reassemble what I want?
LVL 22
Russ SuterSenior Software DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What are the rules for splitting?
Russ SuterSenior Software DeveloperAuthor Commented:
Whatever they need to be to accomplish the goal.
käµfm³d 👽Commented:
Well I was asking how one (or I) would know where the split should occur. Based on what you shown above you could use:

string[] parts = Regex.Split(source, @"(?<=\w+(?:\.\w+){2})\.(?=.*)");

Open in new window


That only works for exactly for what you've posted above. If your rules are different, then the pattern would need to be tweaked.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Neil RussellTechnical Development LeadCommented:
A random example like that will need a clearly defined set of rules.
Are you saying you always want from the last Period onwards to be a separate string?
Russ SuterSenior Software DeveloperAuthor Commented:
I always want exactly two strings. The first will be everything to the left of the rightmost "." character. The second will be everything to the right of the rightmost "." character.
käµfm³d 👽Commented:
Then you could adjust the pattern to:

string[] parts = Regex.Split(source, @"\.(?=\w+$)");

Open in new window

Gustav BrockCIOCommented:
You can use Split and Join:
string allParts = "First.Second.Third.Fourth";
char separator = '.';
string[] parts = allParts.Split(separator);
int lastIndex = parts.Length - 1;
string firstPart = string.Join(separator.ToString(), parts, 0, lastIndex);
string lastPart = parts[lastIndex];

Open in new window

Will return:

    First.Second.Third
    Fourth

/gustav
louisfrCommented:
string[] parts = Regex.Split(source, @"\.(?=\w+$)");

Open in new window

\w works only for alphanumeric characters. If there is a space or whatever after the last period, you won't get any match. Use [^.] instead:
string[] parts = Regex.Split(source, @"\.(?=[^.]+$)");

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russ SuterSenior Software DeveloperAuthor Commented:
That last Regex pattern did the trick. Although I might replace the "+" with a "*" since I would want to return an empty string as the 2nd part if the string I'm testing ended with a "." character.
käµfm³d 👽Commented:
This is why we ask for specific rules and example when regexes are requested. As you can see above, some of us are better at guessing games than others.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.