我想分割这个字符串以提供我想要的输出
sinXcos(b+c)
输出为
sinX
cos(b+c)
我知道如何分割字符串
200XY
使用
token = 200XY;
String[] mix_token = token.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
但是我怎样才能在像这样的字符串上使用这样的东西
sinXcos(b+c)
或者类似的字符串
sinXcos(b+c)tan(z)
最佳答案
这会起作用..
public static void main(String[] args) {
String text = "sinXcos(b+c)tan(z)";
String patternString1 = "(sin|cos|tan)(?![a-z])\\(?\\w(\\+\\w)?\\)?";
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
O/P:
sinX
cos(b+c)
tan(z)
2. Input :"sinabc(X+y)cos(b+c)tan(z)";
O/P :
cos(b+c)
tan(z)
说明:
小
tring patternString1 = "(sin|cos|tan)(?![a-z])\\(?\\w(\\+\\w)?\\)?";
1. (sin|cos|tan) -->start with (sin or cos or tan)
2. (?:![a-z]) --> negative lookahead. check if the next character is not in between [a to z].
3. \\(?\\w(\\+\\w)?\\)?--> an optional brace followed by an alphabet followed by a "+" and another alphabet.
关于java - 使用特定单词java将字符串拆分为单词数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24349101/