If I wanted to pull all substrings between two characters(general) along a String how would I do that? I also want to keep the first char I match but not the second one.
如果我想在两个字符(一般)之间拉出所有的子字符串,我该怎么做呢?我还希望保持第一个char I匹配,而不是第二个char。
So, for example, if I wanted ot keep the characters between a #
char and either the next whitespace OR next of another char (in this case #
again, but could be anything) and I had a string, say : "hello i'm #chilling#likeAVillain but like #forreal"
例如,如果我想在# char和下一个空格之间或者另一个char之间保持字符(在本例中是#,但是可以是任何东西),我有一个字符串,比如:“你好,我是#chill #likeAVillain,但是像#forreal”
How would I get, say a Set of [#chilling, #likeAVillain, #forreal]
我怎么才能得到一套[# cold, #likeAVillain, #forreal]
I'm having difficulty because of the either/or end substring case - I want the substring starting with #
and ending before the first occurence of either another #
or a whitespace (or the end of the string if neither of those are found)
由于/或结束子字符串的情况,我遇到了困难——我希望以#开头的子字符串在另一个#或空格的第一次出现之前结束(或者字符串的末尾,如果两个都没有找到的话)
Put simplest in sudocode:
把简单的sudocode:
for every String W between [char A, either (char B || char C)) // notice [A,B) - want the
//first to be inclusive
Set.add(W);
2 个解决方案
#1
3
This regex #\\w+
seems to do what you need. It will find #
and all alphanumeric characters after it. Since whitespace is not part of \\w
it will not be included in your match.
这个regex #\ w+似乎可以满足您的需要。它会在后面找到#和所有字母数字字符。因为空格不是\w的一部分,所以不会包含在您的匹配中。
String s = "hello i'm #chilling#likeAVillain but like #forreal";
Pattern p = Pattern.compile("#\\w+");
Matcher m = p.matcher(s);
while (m.find())
System.out.println(m.group());
output:
输出:
#chilling
#likeAVillain
#forreal
#2
0
public static void main(String[] args) throws Exception{
String s1 = "hello i'm #chilling#likeAVillain but like #forreal";
String[] strArr = s1.split("\\#");
List<String> strOutputArr = new ArrayList<String>();
int i = 0;
for(String str: strArray){
if(i>0){
strOutputArray.add("#" + str.split("\\s+")[0]);
}
i++;
}
System.out.println(strOutputArray.toString());
}
#1
3
This regex #\\w+
seems to do what you need. It will find #
and all alphanumeric characters after it. Since whitespace is not part of \\w
it will not be included in your match.
这个regex #\ w+似乎可以满足您的需要。它会在后面找到#和所有字母数字字符。因为空格不是\w的一部分,所以不会包含在您的匹配中。
String s = "hello i'm #chilling#likeAVillain but like #forreal";
Pattern p = Pattern.compile("#\\w+");
Matcher m = p.matcher(s);
while (m.find())
System.out.println(m.group());
output:
输出:
#chilling
#likeAVillain
#forreal
#2
0
public static void main(String[] args) throws Exception{
String s1 = "hello i'm #chilling#likeAVillain but like #forreal";
String[] strArr = s1.split("\\#");
List<String> strOutputArr = new ArrayList<String>();
int i = 0;
for(String str: strArray){
if(i>0){
strOutputArray.add("#" + str.split("\\s+")[0]);
}
i++;
}
System.out.println(strOutputArray.toString());
}