0

I'm trying to find a way to split a string by its letters and numbers but I've had luck.

An example: I have a string "AAAA000343BBB343"

I am either needing to split it into 2 values "AAAA000343" and "BBB343" or into 4 "AAAA" "000343" "BBB" "343"

Any help would be much appreciated

Thanks

4
  • 2
    Regex is your friend here. Commented Jul 20, 2016 at 9:49
  • 1
    What have you tried so far? Share you work and we can help from there.. You don't just want a solution but some help, right?? Commented Jul 20, 2016 at 9:49
  • can you explain more about the spliting? how do you decide where to split? Commented Jul 20, 2016 at 9:52
  • stackoverflow.com/questions/1968049/… Commented Jul 20, 2016 at 9:53

3 Answers 3

3

Here is a RegEx approach to split your string into 4 values

string input = "AAAA000343BBB343";
string[] result = Regex.Matches(input, @"[a-zA-Z]+|\d+")
                       .Cast<Match>()
                       .Select(x => x.Value)
                       .ToArray(); //"AAAA" "000343" "BBB" "343"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked perfectly thanks :) I really need to research more on Regular Expressions
2

So you can use regex

For

"AAAA000343" and "BBB343"

var regex = new Regex(@"[a-zA-Z]+\d+");
var result = regex
               .Matches("AAAA000343BBB343")
               .Cast<Match>()
               .Select(x => x.Value);

// result outputs: "AAAA000343" and "BBB343"

For

4 "AAAA" "000343" "BBB" "343"

See @fubo answer

Comments

-1

Try this:

var numAlpha = new Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");
var match = numAlpha.Match("codename123");

var Character = match.Groups["Alpha"].Value;
var Integer = match.Groups["Numeric"].Value;

1 Comment

This will only grab the first instance of letters and numbers, not both. You should try it with the provided data, instead of codename123. Good use of named captures, though.

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.