加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

在java字符串拆分后获取拆分值

发布时间:2020-12-15 00:53:45 所属栏目:Java 来源:网络整理
导读:我有一个动态生成的字符串. 我需要根据Relational Operator拆分字符串. 为此,我可以使用分割功能. 现在我还想知道上面提到的正则表达式,基于哪个Relational Operator是实际拆分的字符串. 一个例子, 在输入 String sb = "FEES 200"; 应用 ListString ls = sb.
我有一个动态生成的字符串.

我需要根据Relational Operator拆分字符串.

为此,我可以使用分割功能.

现在我还想知道上面提到的正则表达式,基于哪个Relational Operator是实际拆分的字符串.

一个例子,
在输入

String sb = "FEES > 200";

应用

List<String> ls =  sb.split(">|>=|<|<=|<>|=");
System.out.println("Splitted Strings: "+s);

会给我结果,

Splitted strings: [FEES,200 ]

但期待结果:

Splitted strings: [FEES,200 ]
Splitted Relational Operator: >

解决方法

您可以使用3个捕获组,并为第二组更改:

(*?)(大于= |< = |<> |> |≤)(*).

Regex demo

说明

>(.*?)匹配任何字符零次或多次非贪婪
>(> = |< = |<> |> |<)匹配> =或< =或<>或>或者<
>(.*)匹配任何字符零次或多次

例如:

String regex = "(.*?)(>=|<=|<>|>|<)(.*)";
String string = "FEES >= 200";            
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
if(matcher.find()) {
    System.out.println("Splitted Relational Operator: " + matcher.group(2));
    System.out.println("Group 1: " + matcher.group(1) + " group 3: " + matcher.group(3));
}

Demo java

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读