字符串regex无法分割封闭括号内的单词

我正在用字符串重组法来拆分下面的字符串。

String input = "( Customer.browse == \"Car Loan\" ) AND ( Campaign.period BETWEEN 2400 AND 600 ) "
            + "AND ( Customer.eligibity == TRUE ) AND ( Campaign.campaign_name == \"Browse To Start\") "
            + "AND ( Customer.application_started == \"Car Loan\" ) AND ( Time.currenttime BETWEEN 800 AND 2000 ) "
            + "THEN ( Notification.message == SUPPRESS)";

我的字符串标记器类如下

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

    public class StringRegexClass {

        public ArrayList<String> stringTokenizer(String str) {

            ArrayList<String> tokenList = new ArrayList<String>();
            Pattern pattern = Pattern.compile("[(\")]|\\w+.\\w+.\\w+|\\w+.\\w+|==");
            Matcher matcher = pattern.matcher(str);
            while (matcher.find()) {
                tokenList.add(matcher.group());
            }
            return (tokenList);
        }
    }

当我把字符串传给上面的类时,我得到了如下的输出。

enter image description here

我想把括号里的字符串分割开来 ( Time.currenttime BETWEEN 800 AND 2000 ) and ( Campaign.period BETWEEN 2400 AND 600 ) 如下

enter image description here

我尝试了不同的方法,但都没有成功,请你建议我需要对我的regex表达式进行哪些修改,才能使其成功。

解决方案:

我建议你完整地捕获引用的字符串。

你需要转义 .

你可以使用下面的regex,但要注意它将默默地跳过任何它不认识的东西。

[()]|"[^"]*"|\w+(?:\.\w+)?|==

在Java 4+中:

public static List<String> stringTokenizer2(String str) {
    List<String> tokenList = new ArrayList<>();
    Pattern pattern = Pattern.compile("[()]|\"[^\"]*\"|\\w+(?:\\.\\w+)?|==");
    for (Matcher matcher = pattern.matcher(str); matcher.find(); )
        tokenList.add(matcher.group());
    return tokenList;
}

在Java 9+中:

public static List<String> stringTokenizer(String str) {
    return Pattern.compile("[()]|\"[^\"]*\"|\\w+(?:\\.\\w+)?|==").matcher(str)
            .results().map(MatchResult::group).collect(Collectors.toList());
}

测试(Java 8)

String input = "( Customer.browse == \"Car Loan\" ) AND ( Campaign.period BETWEEN 2400 AND 600 ) AND ( Customer.eligibity == TRUE ) AND ( Campaign.campaign_name == \"Browse To Start\") AND ( Customer.application_started == \"Car Loan\" ) AND ( Time.currenttime BETWEEN 800 AND 2000 ) THEN ( Notification.message == SUPPRESS)";
for (String token : stringTokenizer(input))
    System.out.println(token);

产量

(
Customer.browse
==
"Car Loan"
)
AND
(
Campaign.period
BETWEEN
2400
AND
600
)
AND
(
Customer.eligibity
==
TRUE
)
AND
(
Campaign.campaign_name
==
"Browse To Start"
)
AND
(
Customer.application_started
==
"Car Loan"
)
AND
(
Time.currenttime
BETWEEN
800
AND
2000
)
THEN
(
Notification.message
==
SUPPRESS
)

给TA打赏
共{{data.count}}人
人已打赏
未分类

GraphQL skipaccess on FIELD_DEFINITION, RBAC for fields?

2022-9-8 19:26:17

未分类

是否可以使用 VSTSAzure DevOps Release 部署任务来故障转移 Azure 函数?

2022-9-8 19:26:19

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索