[英]Java Regex match everything up to and after
I have strings of the form: something~#~something. 我有以下形式的字符串:something〜#〜something。
Essentially I am using ~#~ as a delimiter, but I would like to only split the first element from the rest of the elements. 本质上,我使用〜#〜作为分隔符,但我只想将第一个元素与其余元素分开。
For instance, if I had the following string: What~#~AGreat~#~Day 例如,如果我有以下字符串:What〜#〜AGreat〜#〜Day
I would like my first match to be "What" and my second match to be "AGreat~#~Day. 我希望第一场比赛是“ What”,第二场比赛是“ AGreat〜#〜Day。
The actual strings I'm working with look like this: A1~#~@NP->_A1 So they're not limited to alphanumeric characters. 我正在使用的实际字符串如下所示:A1〜#〜@ NP-> _ A1因此,它们不仅限于字母数字字符。 Can anyone help? 有人可以帮忙吗? I tried looking around for a similar problem but I can't seem to get it working. 我曾尝试寻找类似的问题,但似乎无法正常工作。
Thanks! 谢谢!
String first = input.substring(0, input.indexOf("~#~"));
String second = input.substring(input.indexOf("~#~") + 3, input.length());
Output: 输出:
first: What
second: AGreat~#~Day
You could also use split(String regex, int limit)
method. 您也可以使用split(String regex, int limit)
方法。 For example this code 例如此代码
public class Test {
public static void main(String[] args) {
String s = "abc.bcd.cde";
System.out.println(Arrays.toString(s.split("\\.", 2)));
}
}
produces the following output: 产生以下输出:
[abc, bcd.cde]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.