gpt4 book ai didi

java - String.split() 不适用于我搜索逗号分隔项目的正则表达式

转载 作者:行者123 更新时间:2023-12-01 19:07:03 24 4
gpt4 key购买 nike

您能告诉我为什么我在 String.matches() 中工作的正则表达式模式不能使用 String.split 分割给定的字符串吗?

这是代码:

String text = "sdf.an@dfgdfg.com;   sdfsdf@fdfd.erff";
String regex = "(\\b[\\w.%-]+@[\\w.]+\.[a-zA-Z]{2,4}\\b)([,;]\\s*\\b[\\w.%-]+@[\\w.]+\.[a-zA-Z]{2,4}\\b)*";
String [] emails = text.split(regex);

电子邮件为空:(

最佳答案

我相信 String.split 需要一个分隔符而不是整个模式。

在你的情况下,你可以简单地把这个

text.split(";\\s*")

关于java - String.split() 不适用于我搜索逗号分隔项目的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9661431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com