在模式匹配中排除数字

Exclude Numbers in pattern match

我正在使用这段代码来测试模式是否匹配。 如果输入包括数字、额外的 space、特殊字符,它应该会抛出错误。 (如果模式不匹配。)

为此,我编写了以下代码。

Pattern pattern = Pattern.compile("[^A-Za-z{',-}]");
Matcher match = pattern.matcher("1xyz");

在输出中,我得到 There is a no special character in my string for first name。 在模式匹配中,我只给出匹配当且仅当我们有字母 -,' 作为特殊字符,但它将接受数字也作为接受不知道为什么。

谁能帮我解决这个问题?

It should throw an error if the input includes Numbers, extra space, special characters.

...

In pattern match I have only given match iff we have letters, -,' as special characters,

你可以反过来做,即使用模式,[0-9-,'\s] 表示数字或 -,' 或空白字符。

演示:

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

public class Main {
    public static void main(String[] args) {
        // Test-1
        checkName("1xyz");

        // Test-2
        checkName("xyz");

        // Test-3
        checkName("Hello World");
    }

    static void checkName(String name) {
        Pattern pattern = Pattern.compile("[0-9-,'\s]");
        Matcher match = pattern.matcher(name);
        if (match.find()) {
            System.out.println("There is a digit, whitespace or a special character in the name.");
        } else {
            System.out.println("The name is valid.");
        }
    }
}

输出:

There is a digit, whitespace or a special character in the name.
The name is valid.
There is a digit, whitespace or a special character in the name.

更新(根据 OP 的评论):

如果名字有以下任何一项,你想将其标记为无效

  1. 一个数字
  2. a punctuation/special character from !"#$%&'()*+,-./:;<=>?@[]^_`{|}~
  3. 一个空白字符

你应该使用正则表达式,[0-9\p{Punct}\s]

演示:

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

public class Main {
    public static void main(String[] args) {
        // Test strings
        String[] arr = { "1xyz", "xyz", "Hello World", "He^llo", "James@Bond" };
        for (String s : arr) {
            checkFirstName(s);
        }

    }

    static void checkFirstName(String name) {
        Pattern pattern = Pattern.compile("[0-9\p{Punct}\s]");
        Matcher match = pattern.matcher(name);
        if (match.find()) {
            System.out.println(name + " is not a valid first name.");
        } else {
            System.out.println(name + " is a valid first name.");
        }
    }
}

输出:

1xyz is not a valid first name.
xyz is a valid first name.
Hello World is not a valid first name.
He^llo is not a valid first name.
James@Bond is not a valid first name.

另一个更新(基于 OP 的进一步评论):

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

public class Main {
    public static void main(String[] args) {
        // Test strings
        String[] arr = { "1xyz", "xyz", "Hello World", "He^llo", "James@Bond", "L'Oreal", "sai-praveena",
                "sai-praveena-m", "O'ClockL'Oreal" };
        for (String s : arr) {
            checkFirstName(s);
        }

    }

    static void checkFirstName(String name) {
        Pattern pattern = Pattern.compile("[\p{L}]+[-']?[\p{L}]+");
        Matcher match = pattern.matcher(name);
        if (!match.matches()) {
            System.out.println(name + " is not a valid first name.");
        } else {
            System.out.println(name + " is a valid first name.");
        }
    }
}

输出:

1xyz is not a valid first name.
xyz is a valid first name.
Hello World is not a valid first name.
He^llo is not a valid first name.
James@Bond is not a valid first name.
L'Oreal is a valid first name.
sai-praveena is a valid first name.
sai-praveena-m is not a valid first name.
O'ClockL'Oreal is not a valid first name.

正则表达式,[\p{L}]+[-']?[\p{L}]+ 表示多个字母之一(即 [\p{L}]+)后跟可选的 -',后者依次跟一个或多个字母.