简体   繁体   English

如何拆分字符串(基于各种分隔符)但不保留空格?

[英]How to split a string (based on a variety of delimiters) but without keeping whitespace?

I have Java strings which are boolean expressions with parentheses, & , |我有 Java 字符串,它们是带括号的 boolean 表达式, & , | , and ! , 和! as operators, and I want to split them into tokens.作为运营商,我想将它们拆分为令牌。 For example:例如:

((!A1)&(B2|C3)) should become "(","(",",","A1",")","&","(","B2","|","C3",")",")" ((!A1)&(B2|C3))应该变成"(","(",",","A1",")","&","(","B2","|","C3",")",")"

Following this answer I found that I can use Java's String.split() with a regex that includes lookahead and lookbehind clauses:按照这个答案,我发现我可以将 Java 的String.split()与包含前瞻和后瞻子句的正则表达式一起使用:

List<String> tokens = "((!A1)&(B2|C3))".split("((?<=[!&()|])|(?=[!&()|]))")

My only problem is that whitespace will be included in the list of tokens.我唯一的问题是空格将包含在令牌列表中。 For example if I were to write the expression as ( ( !A1 ) & ( B2 | C3 ) ) then my split() would produce at least four strings like " " and there'd be padding around my variables (eg " A1 " ).例如,如果我将表达式写为( ( !A1 ) & ( B2 | C3 ) )那么我的split()将产生至少四个像" "的字符串,并且我的变量周围会有填充(例如" A1 " )。

How can I modify this split expression and regex to tokenize the string but not keep any of the witespace?如何修改此split表达式和正则表达式以标记字符串但不保留任何空白空间?

Instead of split you can use this this regex to match what you want:您可以使用这个正则表达式而不是 split 来匹配您想要的内容:

[!&()]|[^!&()\h]+

RegEx Demo正则表达式演示

RegEx Details:正则表达式详细信息:

  • [!&()] : Match ! [!&()] : 匹配! or & or ( or )&()
  • | : OR : 或者
  • [^!&()\h]+ : Match any characters that is NOT ! [^!&()\h]+ :匹配任何不是的字符! , & , ( , ) and a whitespace , & , ( , )和一个空格

Code:代码:

final String regex = "[!&()]|[^!&()\\h]+";
final String string = "((!A1)&( B2 | C3 ))";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

List<String> result = new ArrayList<>();
while (matcher.find()) {
    result.add(matcher.group(0));
}

System.out.println(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM