


Java regular expression syntax example: matching email and mobile phone numbers, specific code examples are required
Regular expression is a powerful text matching tool that can be used Extract and match required information from text. In Java, using regular expressions requires using the related classes and methods provided by the java.util.regex package. This article will introduce how to use regular expressions to match email addresses and mobile phone numbers, and give specific code examples.
1. Matching email addresses
The format of email addresses is usually "username@domain name", in which both username and domain name have certain rules and restrictions. The following is an example of a regular expression to match common email formats:
String emailRegex = "\w+@(\w+\.)+[a-zA-Z]{2,4}";
where:
- "\w " matches one or more letters, numbers, or underscores, Represents the part of the user name;
- "(\w .) " matches one or more letters, numbers, or underscores followed by a dot, representing the part of the domain name;
- "[a-zA- Z]{2,4}" matches 2 to 4 letters, indicating the domain name suffix.
Use the above regular expression to quickly determine whether a string conforms to the email format. The sample code is as follows:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class EmailValidator { private static final String EMAIL_REGEX = "\w+@(\w+\.)+[a-zA-Z]{2,4}"; public static boolean isValidEmail(String email) { Pattern pattern = Pattern.compile(EMAIL_REGEX); Matcher matcher = pattern.matcher(email); return matcher.matches(); } public static void main(String[] args) { String email = "test123@example.com"; if (isValidEmail(email)) { System.out.println(email + "是一个有效的邮箱地址"); } else { System.out.println(email + "不是一个有效的邮箱地址"); } } }
Run the above code to get the following output:
test123@example.com是一个有效的邮箱地址
2. Matching mobile phone numbers
Mobile phone numbers generally consist of 11 digits, starting with 1. The following is an example of a regular expression to match the common mobile phone number format:
String phoneRegex = "1[3-9]\d{9}";
where:
- "1" means the mobile phone number starts with 1;
- "[3-9]" means that the second digit can be any number between 3 and 9;
- "\d{9}" means that the following 9 digits can be any number.
Use the above regular expression to quickly determine whether a string conforms to the mobile phone number format. The sample code is as follows:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class PhoneValidator { private static final String PHONE_REGEX = "1[3-9]\d{9}"; public static boolean isValidPhone(String phone) { Pattern pattern = Pattern.compile(PHONE_REGEX); Matcher matcher = pattern.matcher(phone); return matcher.matches(); } public static void main(String[] args) { String phone = "13812345678"; if (isValidPhone(phone)) { System.out.println(phone + "是一个有效的手机号码"); } else { System.out.println(phone + "不是一个有效的手机号码"); } } }
Run the above code to get the following output:
13812345678是一个有效的手机号码
Summary:
Through the above example code, we show how to use regular expressions to match email addresses and mobile phone numbers. Regular expressions are a powerful tool that can be used to handle a variety of text matching problems. In practical applications, we can adjust the specific rules of regular expressions as needed to adapt to various data format requirements. I hope this article helps you learn and use regular expressions.
The above is the detailed content of Example: Use Java regular expressions to match email addresses and mobile phone numbers. For more information, please follow other related articles on the PHP Chinese website!

mail.ru邮箱是俄罗斯地区的用户经常是使用的,有很多的国内的用户比较好奇了mail.ru是什么邮箱呢?现在就来看一下小编给大家带来的mail.ru邮箱介绍吧。mail.ru是什么邮箱答案:来自俄罗斯的邮箱服务。1、mail.ru邮箱网页版地址:https://mail.ru/;2、我们进入到mail.ru邮箱网页版的首页页面中我们可以在左侧看到有【@mail】点击;3、然后点击过后就会来到mail.ru邮箱的登录页面中来,在这里我们输邮箱地址之后,即可登录邮箱,如果没有账号的话点击【Crea

在当今互联网时代,电子邮件依然是人们最常用的沟通工具之一。无论是个人还是企业,都需要一个高效可靠的邮件发送系统。PHP作为一种广泛使用的编程语言,在开发邮件发送系统方面具有很大的优势。本文将介绍如何使用PHP构建一个高效的邮件发送系统。首先,我们需要配置PHP环境。确保服务器上已经安装了PHP,并且启用了相关的扩展模块,如mbstring、op

大家好!我是虎哥。项目背景作为数据分析师,我们需要经常制作统计分析图表。但是报表太多的时候往往需要花费我们大部分时间去制作报表。这耽误了我们利用大量的时间去进行数据分析。但是作为数据分析师我们应该尽可能去挖掘表格图表数据背后隐藏关联信息,而不是简单的统计表格制作图表再发送报表。一、报表自动化目的1.节省时间,提高效率自动化总是能够很好的节省时间,提高我们的工作效率。让我们的程序编程尽可能的降低每个功能实现代码的耦合性,更好的维护代码。这样我们会节省很多时间让我们有空去做更多有价值有意义的工作。2

企业微信的邮箱怎么登陆?企业微信APP中是可以登陆邮箱,但是多数的用户不知道邮箱如何的登陆,接下来就是小编为用户带来的企业微信邮箱登陆方法图文教程,感兴趣的用户快来一起看看吧!企业微信使用教程企业微信的邮箱怎么登陆1、首先打开企业微信APP,进入到主页面最底部【工作台】点击来专区;2、之后在工作台专区中,选择其中的【企业邮箱】服务;3、然后跳转到企业邮箱功能页,点击底部的【绑定】或者【换一个邮箱】;4、最后在下图所示的页面输入【QQ账号】和【密码】即可登陆邮箱。

一个手机号最多有1个163邮箱,因为在创建163邮箱时,需要绑定一个有效的手机号,该手机号会被认证并作为账户的一部分进行记录,如果您希望创建另一个163邮箱,则需要另外一个未曾用于绑定其他163邮箱的不同手机号来进行认证。

1、电子邮件地址的格式是【用户名@域名】,其中用户名是收件人的账号,域名是收件人的电子邮件服务器的名称。2、例子【123456@qq.com】就是一个电子邮件地址,其中【123456】是用户名,【qq.com】是域名。3、再举个例子【888888@163.com】,其中【888888】是用户名,【163.com】是域名。

1、首先,用户需要选择一个合适的邮箱服务商,如腾讯、网易、新浪等。2、打开其官网的注册页面,找到并点击【注册】或【创建账户】(类似选项)选项。3、在注册页面中,填写准确的信息,包括用户名、密码、手机号、验证码等。4、点击注册按钮,完成注册操作,然后打开刚刚注册的邮箱,按照提示设置邮箱的昵称、密码等基本信息即可。

1、QQ邮箱输入的正确格式是:QQ号@qq.com。2、163邮箱输入的正确格式是:用户名@163.com。3、139邮箱输入的正确格式是:用户名@139.com。4、126邮箱输入的正确格式是:用户名@126.com。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
