java - 电子邮件的正则表达式,其间应包含连字符,但不能在开始和结束位置
问题描述
我有一个满足条件的正则表达式,但它允许在开头使用连字符。一开始怎么限制。我想要在开头限制连字符的相同正则表达式,因为它允许我需要的其他一些字符。
/^[A-Z0-9-._%+]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
提前致谢
解决方案
这似乎对我有用
public class QuickyTest{
static String[] testVectors = { "alaskjf",
"o-kay@example.com",
"-dash@x.AA",
"dash-@y.ZZ" };
public static void main( String[] args ) {
Pattern pattern = Pattern.compile( "(?i:[A-Z0-9._%+]+|[A-Z0-9._%+]+[A-Z0-9._%+-]+[A-Z0-9._%+]+@([A-Z0-9-]+.)+[A-Z]{2,4})" );
for( String test : testVectors ) {
Matcher match = pattern.matcher( test );
System.out.println( test + " : " + match.matches() );
}
}
}
推荐阅读
- react-native - 迭代以显示图像
- python - 从 Django 中的 QuerySet 中排除对象实例的最佳方法是什么(在这种情况下 filter() 不是一个选项)?
- javascript - 在 twitter 按钮图标旁边显示点赞数
- android - 用户收到奖励后以编程方式关闭奖励广告
- algorithm - 具有 n 层和 k 部电梯的建筑物中的最小电梯问题
- python - 如何向 kafka 主题的所有消费者发送相同的消息?
- excel - Excel:如何使用索引匹配公式动态查找行
- reactjs - 在 MongoDb 中保存 isoDate 时的小时数问题
- android - 在 Android 模拟器上将 sip 呼叫(TeleService)添加到 AOSP
- python - 如何从字典列表中获取价值?