java - 正则表达式在字符串中查找 @ 符号
问题描述
我需要我的正则表达式帮助,以便它可以找到我正在搜索的字符串中是否有 @ 符号。
import java.util.regex.*;
public class OnlineNewspaperSubscription extends NewspaperSubscription
{
public void setAddress(String a)
{
address = a;
// Creating a pattern from regex
Pattern pattern
= Pattern.compile("@*");
// Get the String to be matched
String stringToBeMatch = a;
// Create a matcher for the input String
Matcher matcher = pattern.matcher(stringToBeMatch);
if(matcher.matches())
{
super.rate = 9;
}
else
{
super.rate = 0;
System.out.println("Need an @ sign");
}
}
}
我应该能够判断这个字符串是否是电子邮件地址。
解决方案
You don't need a regular expression to find the index of '@'
in a String
; use String.indexOf(int)
(passing a char
). Like,
int p = a.indexOf('@');
if (p > -1) {
// ...
}
推荐阅读
- python - 如何使用中间层的结果(数组)切片
- xamarin - Xamarin iOS 绑定构建没有错误,但在启动时崩溃,没有输出
- ansible - Ansible:APT 模块:如何获取标准输出?
- ruby - 错误脚本录制声音
- swift - Swift:检测何时显示应用内购买的购买提醒
- spring - Mongodb 聚合仅在 Spring Data 上很慢
- php - php邮件功能错误-收件箱中仅显示我的客户列表的最后一个电子邮件地址
- sql - ORA-02069: 必须将此操作的 global_names 参数设置为 TRUE
- android - 在firebase中显示数据以进行颤动
- angular6 - ./node_modules/pdfjs-dist/build/pdf.js 中的警告未找到模块:错误:无法解析“ui_angular_6\node_modules\pdfjs-dist\build”中的“zlib”