Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Cách khớp n lần xuất hiện của biểu thức dùng Java RegEx

Các quantifier tham lam (greedy quantifiers) do Java cung cấp cho phép bạn khớp nhiều lần xuất hiện của một biểu thức. Trong đó:

  • Exp{n} bắt buộc biểu thức exp xuất hiện đúng n lần.
  • Exp{n,} bắt buộc biểu thức exp xuất hiện ít nhất n lần.
  • Exp{n, m} bắt buộc biểu thức exp xuất hiện ít nhất n và nhiều nhất m lần.

Ví dụ 1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExample {
    public static void main(String args[]) {
        // Biểu thức chính quy chấp nhận từ 5 ký tự
        String regex = "\\w{5}";
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhập 5 chuỗi đầu vào: ");
        String input[] = new String[5];
        for (int i = 0; i < 5; i++) {
            input[i] = sc.nextLine();
        }
        // Tạo đối tượng Pattern
        Pattern p = Pattern.compile(regex);
        for (int i = 0; i < 5; i++) {
            // Tạo đối tượng Matcher
            Matcher m = p.matcher(input[i]);
            if (m.find()) {
                System.out.println(input[i] + ": accepted");
            } else {
                System.out.println(input[i] + ": not accepted");
            }
        }
    }
}

Kết quả

Nhập 5 chuỗi đầu vào: rama raja raghu megha malya
rama: not accepted
raja: not accepted
raghu: accepted
megha: accepted
malya: accepted

Ví dụ 2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExample {
    public static void main(String args[]) {
        // Biểu thức chính quy khớp chuỗi non-word độ dài 2 đến 6
        String regex = "\\W{2,6}";
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhập 5 chuỗi đầu vào: ");
        String input[] = new String[5];
        for (int i = 0; i < 5; i++) {
            input[i] = sc.nextLine();
        }
        // Tạo đối tượng Pattern
        Pattern p = Pattern.compile(regex);
        for (int i = 0; i < 5; i++) {
            // Tạo đối tượng Matcher
            Matcher m = p.matcher(input[i]);
            if (m.find()) {
                System.out.println(input[i] + " matched");
            }
        }
    }
}

Kết quả 1

Nhập 5 chuỗi đầu vào: hello how are you #$#% # #$@%%#&#& sample text
#$#% matched
#$@%%#&#& matched

Ví dụ 3

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExample {
    public static void main(String args[]) {
        String regex = "[a-zA-Z]{1,20}";
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhập tên học sinh:");
        String name = sc.nextLine();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(name);
        if (m.matches()) {
            System.out.println("Tên phù hợp");
        } else {
            System.out.println("Tên không phù hợp");
        }
    }
}

Kết quả

Nhập tên học sinh: Mouktika
Tên phù hợp