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 21
Russ SuterAsked:
Who is Participating?
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?
0
Russ SuterAuthor Commented:
Whatever they need to be to accomplish the goal.
0
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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?
0
Russ SuterAuthor 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.
0
käµfm³d 👽Commented:
Then you could adjust the pattern to:

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

Open in new window

0
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
0
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

0

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 SuterAuthor 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.
0
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.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.