Java 如何在字符串中查找模式并删除所有不符合';不匹配
如何删除字符串行中的所有内容并获取与模式匹配的信息 例如,我有一些字符串作为输入:Java 如何在字符串中查找模式并删除所有不符合';不匹配,java,android,regex,Java,Android,Regex,如何删除字符串行中的所有内容并获取与模式匹配的信息 例如,我有一些字符串作为输入: “废话myexample@ex.com" 我只想收到电子邮件 或 “公司BLBAL电话:88 99 99 247传真:99 88 14 574” 我只想知道电话号码 我正在使用模式检查字符串是否包含电子邮件或数字,但我不知道如何从中删除所有其他字符串,并仅获取匹配的内容 一些建议或例子 int start,end,length; String text ="bleble blabbl myexample@ex.c
int start,end,length;
String text ="bleble blabbl myexample@ex.com blabla"
Pattern emailP = Pattern.compile(".+@.+\\.com");
Matcher matcherEmail =emailP.matcher(text);
if (matcherEmail.find()) {
start=matcherEmail.start();
//substring
tekst=tekst.substring(0,start);
Matcher matcherEmail =emailP.matcher(text);
end=matcherEmail.end();
length=text.length();
tekst=text.substring(end,length);
}
那么,会是这样吗?
删除匹配模式之前和之后的所有内容
我需要检查字符串2次?您的正则表达式
++.++\.com
匹配任何字符1+次(包括空格)、一个@
和一个+字符(包括空格)。请注意,+
是贪婪的,将匹配到字符串末尾,并且只匹配一个电子邮件地址
一种不删除非电子邮件地址的方法是匹配并使用\S
而不是匹配非空白字符的+
:
\S+@\S+\.com\b
在Java中:
String regex = "\\S+@\\S+\\.com\\b";
String text ="bleble blabbl myexample@ex.com blabla";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
|
要获取电话号码,您可以将a与a结合使用。请向我们展示您尝试过的方法以及失败的原因。另外,一个最小的、可编译和可验证的示例()也会很有帮助