tags:

views:

320

answers:

6

I need to learn RegEx but don't have time to figure this out right now. -- So I'm attempting exploit the community's capabilities.

I have a string containing a list of acceptable 1-character comment variables.

String comments = "#;";

And I want:

String[] parsedComments = {"#", ";"};

What RegEx string will solve my problems?

String[] parsedComments = comments.split(/*  "???"  */);
+1  A: 

Just access the string index using comments[0], comments[1] ... comments[n]

Lazarus
Not in Java; see cjstehno's answer.
Alan Moore
Too many languages on the go :)
Lazarus
+2  A: 

I misunderstood you in my original answer. You don't want Regex. Regex is used to find patterns, and you just want to split. You could use an empty string, but that will return an empty string as well as the characters.

Just access through the index.

Daniel
+3  A: 

This should do it, among other weird hacks:

String[] parsedComments = comments.split("(?!^)");

It's hardly a job for regex, though. May as well just iterate across the string and build an array out of each 1-character substring.

chaos
Not quite - I get a result of ["", "#", ";"] which can just as well be obtained by using "" as the argument to `split`.
Vinay Sajip
Oy. I'm testing the regex in Perl, and it doesn't do that. Edited to possibly suppress the empty string.
chaos
"(?<!^)" alone works just as well. In fact, it doesn't even have to be a lookbehind: "(?!^)" works, too.
Alan Moore
Well okay then. :)
chaos
+4  A: 

First, why do you need to break them into an array? A String has almost the exact same methods available to getting chars at indexes and virtually the same performance.

But, to answer your question:

string.split("");

This will give you an extra empty string at the first pos of the resulting array.

jjnguy
Thanks. It looks like I was trying to make it waay more complicated than it needed to be.
Nate
+11  A: 

Why do you want to use a regex? Try String.toCharArray() for example.

soulmerge
You just skeeted me.
Martinho Fernandes
I agree that using regexes is overkill, but your example gives a `char []` result rather than a `String[]` result.
Vinay Sajip
That is true. But a character array is a much better data structure for storing - well - an array of characters, unless, of course, you absolutely need a string array for some obscure reasons. But any local code can be updated to work with the more intuitive `char[]` type.
soulmerge
+2  A: 

I tried to add this as a comment to "Daniel" and "Lazarus", but I don't have enough reputation points yet... unless I am misunderstanding you, you are saying to access the original string using the index. You cannot do that in Java.

String foo = "abcde";
String bee = foo[1]; // not valid

If I have misunderstood you, I apologize. If not, I wanted to clarify that for posterity. :-)

cjstehno
You're right: a Java String is not just an array of char's. You *can* access a character by its index, but you do it by means of the charAt() method. (Or codePointAt() if you want to allow for surrogate pairs.)
Alan Moore