I want to split a string like "001A" into "001" and "A"
+1
A:
Are the numbers and letters always going to be separate like that? Or can you potentially have something like 00A1?
jaltiere
2010-04-15 14:06:47
This should be a comment to the question, not an answer.
Oded
2010-04-15 14:09:13
I would have put it as a comment to the question, but I'm not sure how. I don't see a comment link available to me.
jaltiere
2010-04-15 14:13:32
As a new user, you cannot leave comments. Sorry. It will come. Welcome to Stack Overflow.
Kobi
2010-04-15 14:16:52
It doesn't make sense that new users can't comment on posts from other users but they can comment on their own posts :s
James
2010-04-15 14:19:45
I agree. Until I get permission to do this, I find myself sometimes responding in my own comment to a comment placed somewhere else so I don't get dinged for asking for clarification in an answer. :)
jaltiere
2010-04-15 14:26:41
@jaltiere: just say in your answer **not enough rep to post comment**. People will understand :)
James
2010-04-15 14:28:57
+3
A:
Match match = Regex.Match(s, @"^(\d+)(.+)$");
string numeral = match.Groups[1].Value;
string tail = match.Groups[2].Value;
Kobi
2010-04-15 14:09:56
@knittl - I know, `.` is ok as long as the OP doesn't need to validate it. The question doesn't have enough details so I went with this.
Kobi
2010-04-15 17:07:51
the title of the question is »how to split numerics and _alphabets_« ;) but nevermind, in the given example your solution will give correct results
knittl
2010-04-15 18:02:49
+2
A:
string[] data = Regex.Split("001A", "([A-Z])");
data[0] -> "001"
data[1] -> "A"
James
2010-04-15 14:10:05
Did you test it? It should be `Regex.Split("001A", "([A-Z])")`, or the group is removed (as a divider).
Kobi
2010-04-15 14:16:02
A:
You could try something like this to retrieve the integers from the string:
StringBuilder sb = new StringBuilder();
Regex regex = new Regex(@"\d*");
MatchCollection matches = regex.Matches(inputString);
for(int i=0; i < matches.count;i++){
sb.Append(matches[i].value + " ");
}
Then change the regex to match on characters and perform the same loop.
derek
2010-04-15 14:23:33
+1
A:
If your code is as simple|complicated as your 001A sample, your should not be using a Regex but a for-loop.
FireSnake
2010-04-15 14:33:14
A:
And if there's more like 001A002B
then you could
var s = "001A002B";
var matches = Regex.Matches(s, "[0-9]+|[A-Z]+");
var numbers_and_alphas = new List<string>();
foreach (Match match in matches)
{
numbers_and_alphas.Add(match.Value);
}
Jonas Elfström
2010-04-15 14:38:41
+1
A:
This is Java, but it should be translatable to other flavors with little modification.
String s = "123XYZ456ABC";
String[] arr = s.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");
System.out.println(Arrays.toString(arr));
// prints "[123, XYZ, 456, ABC]"
As you can see, this splits a string wherever \d
is followed by a \D
or vice versa. It uses positive and negative lookarounds to find the places to split.
polygenelubricants
2010-04-15 15:44:19