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.
我需要学习RegEx,但现在没有时间去解决这个问题。 - 所以我试图利用社区的能力。
I have a string containing a list of acceptable 1-character comment variables.
我有一个字符串,其中包含可接受的1个字符的注释变量列表。
String comments = "#;";
And I want:
而且我要:
String[] parsedComments = {"#", ";"};
What RegEx string will solve my problems?
什么RegEx字符串将解决我的问题?
String[] parsedComments = comments.split(/* "???" */);
6 个解决方案
#1
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.
首先,为什么需要将它们分成数组? String几乎有完全相同的方法可用于获取索引的字符和几乎相同的性能。
But, to answer your question:
但是,回答你的问题:
string.split("");
This will give you an extra empty string at the first pos of the resulting array.
这将在结果数组的第一个pos处为您提供额外的空字符串。
#2
Why do you want to use a regex? Try String.toCharArray() for example.
为什么要使用正则表达式?例如,尝试使用String.toCharArray()。
#3
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.
然而,这对于正则表达式来说几乎不是一项工作。也可以迭代遍历字符串并从每个1个字符的子字符串中构建一个数组。
#4
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.
我试图将此作为对“Daniel”和“Lazarus”的评论添加,但我还没有足够的声誉点......除非我误解你,否则你说要使用索引访问原始字符串。你不能用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. :-)
如果我误解了你,我道歉。如果没有,我想为后人澄清这一点。 :-)
#5
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.
只需通过索引访问。
#6
Just access the string index using comments[0], comments[1] ... comments[n]
只需使用comments [0],comments [1] ... comments [n]访问字符串索引
#1
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.
首先,为什么需要将它们分成数组? String几乎有完全相同的方法可用于获取索引的字符和几乎相同的性能。
But, to answer your question:
但是,回答你的问题:
string.split("");
This will give you an extra empty string at the first pos of the resulting array.
这将在结果数组的第一个pos处为您提供额外的空字符串。
#2
Why do you want to use a regex? Try String.toCharArray() for example.
为什么要使用正则表达式?例如,尝试使用String.toCharArray()。
#3
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.
然而,这对于正则表达式来说几乎不是一项工作。也可以迭代遍历字符串并从每个1个字符的子字符串中构建一个数组。
#4
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.
我试图将此作为对“Daniel”和“Lazarus”的评论添加,但我还没有足够的声誉点......除非我误解你,否则你说要使用索引访问原始字符串。你不能用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. :-)
如果我误解了你,我道歉。如果没有,我想为后人澄清这一点。 :-)
#5
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.
只需通过索引访问。
#6
Just access the string index using comments[0], comments[1] ... comments[n]
只需使用comments [0],comments [1] ... comments [n]访问字符串索引