个性化阅读
专注于IT技术分析

Java正则表达式

点击下载

本文概要

Java的正则表达式或正则表达式定义的模式用于搜索或处理字符串的API。

它被广泛用于定义字符串,如密码和电子邮件验证约束。学习Java的正则表达式教程后,你将能够在Java正则表达式测试工具来测试你的正则表达式。

正则表达式的Java API提供1个接口和3类中java.util.regex包。

java.util.regex包

匹配器和模式类提供的Java正则表达式的设施。该java.util.regex包提供了以下类和接口的正则表达式。

  1. MatchResult的接口
  2. Matcher类
  3. Pattern类
  4. PatternSyntaxException类

Matcher类

它实现了MatchResult的接口。它是用于对一个字符序列执行匹配操作的正则表达式引擎。

没有。方法描述
1boolean matches()测试该正则表达式是否与模式匹配。
2boolean find()发现该模式匹配的下一个表达式。
3boolean find(int start)发现从给定的开始数的模式匹配,下一个表达式。
4String group()返回匹配的序列。
5int start()返回匹配的子序列的起始索引。
6int end()返回匹配的子序列的结束索引。
7int groupCount()返回匹配子序列的总数。

Pattern类

这是一个正则表达式的编译版本。它被用于定义正则表达式引擎的图案。

没有。方法描述
1static Pattern compile(String regex)编译正则表达式给出,并返回模式的实例。
2Matcher matcher(CharSequence input)创建与所述图案的给定的输入相匹配的匹配器。
3static boolean matches(String regex,CharSequence input)它的工作原理的编译和匹配相结合的方法。它编译正则表达式和与图案的给定的输入相匹配。
4String[] split(CharSequence input)拆分给周围模式的匹配给定的输入字符串。
5String pattern()返回正则表达式模式。

Java正则表达式的例子

有三种方法使用Java编写正则表达式的例子。

import java.util.regex.*;
public class RegexExample1{
public static void main(String args[]){
//1st way
Pattern p = Pattern.compile(".s");//. represents single character
Matcher m = p.matcher("as");
boolean b = m.matches();

//2nd way
boolean b2=Pattern.compile(".s").matcher("as").matches();

//3rd way
boolean b3 = Pattern.matches(".s","as");

System.out.println(b+" "+b2+" "+b3);
}}

产量

true true true

正则表达式 。例

的。 (点)表示一个字符。

import java.util.regex.*;
class RegexExample2{
public static void main(String args[]){
System.out.println(Pattern.matches(".s","as"));//true (2nd char is s)
System.out.println(Pattern.matches(".s","mk"));//false (2nd char is not s)
System.out.println(Pattern.matches(".s","mst"));//false (has more than 2 char)
System.out.println(Pattern.matches(".s","amms"));//false (has more than 2 char)
System.out.println(Pattern.matches("..s","mas"));//true (3rd char is s)
}}

正则表达式字符类

没有。字符类描述
1[abc]的a,b,或c(简单类)
2[^abc]除了a,b或c(否定)任何字符
3[a-zA-Z]一个穿过Z Z或A,包括(范围)
4[a-d[m-p]]a到d,或通过P M:[α-DM-p](显示集)
5[a-z&&[def]]d,e或F(交叉点)
6[a-z&&[^bc]]a到z,除了b和c:[AD-Z](减法)
7[a-z&&[^m-p]]a到z,而不是通过P M:[A-LQ-Z](减法)

正则表达式字符类例

import java.util.regex.*;
class RegexExample3{
public static void main(String args[]){
System.out.println(Pattern.matches("[amn]","abcd"));//false (not a or m or n)
System.out.println(Pattern.matches("[amn]","a"));//true (among a or m or n)
System.out.println(Pattern.matches("[amn]","ammmna"));//false (m and a comes more than once)
}}

正则表达式量词

量词指定字符的出现次数。

正则表达式描述
X?X只出现一次或根本不出现
X +X出现一次或多次
X*X发生零次或多次
X{n}X只出现n次
X {N,}X出现n次或更多次
X{y,z}X发生至少y倍的,但小于z乘以

正则表达式字符类和量词实施例

import java.util.regex.*;
class RegexExample4{
public static void main(String args[]){
System.out.println("? quantifier ....");
System.out.println(Pattern.matches("[amn]?","a"));//true (a or m or n comes one time)
System.out.println(Pattern.matches("[amn]?","aaa"));//false (a comes more than one time)
System.out.println(Pattern.matches("[amn]?","aammmnn"));//false (a m and n comes more than one time)
System.out.println(Pattern.matches("[amn]?","aazzta"));//false (a comes more than one time)
System.out.println(Pattern.matches("[amn]?","am"));//false (a or m or n must come one time)

System.out.println("+ quantifier ....");
System.out.println(Pattern.matches("[amn]+","a"));//true (a or m or n once or more times)
System.out.println(Pattern.matches("[amn]+","aaa"));//true (a comes more than one time)
System.out.println(Pattern.matches("[amn]+","aammmnn"));//true (a or m or n comes more than once)
System.out.println(Pattern.matches("[amn]+","aazzta"));//false (z and t are not matching pattern)

System.out.println("* quantifier ....");
System.out.println(Pattern.matches("[amn]*","ammmna"));//true (a or m or n may come zero or more times)

}}

正则表达式元字符

正则表达式元字符上班简码。

正则表达式描述
.任何字符(可能匹配也可能不匹配终止符)
\ d任何数字,不包括[0-9]
\D任何非数字,缩写[^ 0-9]
\s任何空格字符,缩写为[\t\n\x0B\f\r]
\ S任何非空格字符,缩写为[^\s]
\w任何字字符,短和[a-ZA-Z_0-9]
\W任何非单词字符,缩写为[^\w]
\ b一个单词边界
\B非单词边界

正则表达式元字符范例

import java.util.regex.*;
class RegexExample5{
public static void main(String args[]){
System.out.println("metacharacters d....");\\d means digit

System.out.println(Pattern.matches("\\d","abc"));//false (non-digit)
System.out.println(Pattern.matches("\\d","1"));//true (digit and comes once)
System.out.println(Pattern.matches("\\d","4443"));//false (digit but comes more than once)
System.out.println(Pattern.matches("\\d","323abc"));//false (digit and char)

System.out.println("metacharacters D....");\\D means non-digit

System.out.println(Pattern.matches("\\D","abc"));//false (non-digit but comes more than once)
System.out.println(Pattern.matches("\\D","1"));//false (digit)
System.out.println(Pattern.matches("\\D","4443"));//false (digit)
System.out.println(Pattern.matches("\\D","323abc"));//false (digit and char)
System.out.println(Pattern.matches("\\D","m"));//true (non-digit and comes once)

System.out.println("metacharacters D with quantifier....");
System.out.println(Pattern.matches("\\D*","mak"));//true (non-digit and may come 0 or more times)

}}

正则表达式问题1

/*Create a regular expression that accepts alphanumeric characters only. 
Its length must be six characters long only.*/

import java.util.regex.*;
class RegexExample6{
public static void main(String args[]){
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}","arun32"));//true
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}","kkvarun32"));//false (more than 6 char)
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}","JA2Uk2"));//true
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}","arun$2"));//false ($ is not matched)
}}

正则表达式问题2

/*Create a regular expression that accepts 10 digit numeric characters
 starting with 7,8 or 9 only.*/

import java.util.regex.*;
class RegexExample7{
public static void main(String args[]){
System.out.println("by character classes and quantifiers ...");
System.out.println(Pattern.matches("[789]{1}[0-9]{9}","9953038949"));//true
System.out.println(Pattern.matches("[789][0-9]{9}","9953038949"));//true

System.out.println(Pattern.matches("[789][0-9]{9}","99530389490"));//false (11 characters)
System.out.println(Pattern.matches("[789][0-9]{9}","6953038949"));//false (starts from 6)
System.out.println(Pattern.matches("[789][0-9]{9}","8853038949"));//true

System.out.println("by metacharacters ...");
System.out.println(Pattern.matches("[789]{1}\\d{9}","8853038949"));//true
System.out.println(Pattern.matches("[789]{1}\\d{9}","3853038949"));//false (starts from 3)

}}

Java的正则表达式搜索示例

import java.util.regex.Pattern;
import java.util.Scanner;
import java.util.regex.Matcher;  
public class RegexExample8{  
    public static void main(String[] args){  
        Scanner sc=new Scanner(System.in);
        while (true) {  
        	System.out.println("Enter regex pattern:");
            Pattern pattern = Pattern.compile(sc.nextLine());  
            System.out.println("Enter text:");
            Matcher matcher = pattern.matcher(sc.nextLine());  
            boolean found = false;  
            while (matcher.find()) {  
                System.out.println("I found the text "+matcher.group()+" starting at index "+  
                 matcher.start()+" and ending at index "+matcher.end());  
                found = true;  
            }  
            if(!found){  
                System.out.println("No match found.");  
            }  
        }  
    }  
}

输出:

Enter regex pattern: java
Enter text: this is java,do you know java
I found the text java starting at index 8 and ending at index 12
I found the text java starting at index 26 and ending at index 30
赞(0)
未经允许不得转载:srcmini » Java正则表达式

评论 抢沙发

评论前必须登录!