eclipse Java 正则表达式(无空格或 0-9)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11481250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:34:36  来源:igfitidea点击:

Java Regex (No White Space or 0-9)

javaregexeclipse

提问by mnmn

Input, via a question, the report owner's first name as a string.

通过问题输入报告所有者的名字作为字符串。

Need a regular expression to check, conditionally, to make sure the first name doesn't contain any numeric characters, numbers between 0 – 9. If it does you must remove it. The first name can not contain any white space either.

需要一个正则表达式来有条件地检查,以确保名字不包含任何数字字符,0 – 9 之间的数字。如果是,您必须将其删除。名字也不能包含任何空格。

    do
    {
        System.out.println("Please enter your FIRST name:");
        firstName = keyboard.next();
        firstName= firstName.toUpperCase();
    }

    while( !firstName.matches("^/s^[a-zA-Z]+$/s"));

    System.out.println("Thanks " + firstName);


Output
p
Please enter your FIRST name:
p p
Please enter your FIRST name:
Please enter your FIRST name:

回答by Bohemian

You've got your regex muddled up. Try this:

你的正则表达式搞砸了。尝试这个:

while(!firstName.matches("^[^\d\s]+$"));

The regex "^[^\\d\\s]+$"means "non digits or whitespace, and at least one character"

正则表达式的"^[^\\d\\s]+$"意思是“非数字或空格,以及至少一个字符”

回答by Prince John Wesley

As you have firstnamein uppercase and matchesmethod matches the whole input, [A-Z]+is sufficient.

正如您firstname在大写和matches方法中匹配整个输入一样, [A-Z]+就足够了。

while( !firstName.matches("[A-Z]+"));

or

或者

while( !firstName.matches("\p{Lu}+"));

回答by Isuru

Try

尝试

firstName =  firstName.replaceAll("[\d\s]", "").toUpperCase();

回答by shinds

You can try this. Created it using Rubular.com. The

你可以试试这个。使用Rubular.com创建它。这

Pattern nameRequirment = Pattern.compile("^((?!.[\d\s]).)*$");

while (!nameRequirement.matcher(myString).matches()){
  //... prompt for new name
}

回答by Mike S.

If you want to eliminate digits, just force:

如果你想消除数字,只需强制:

\D*

In your matcher

在你的匹配器中

回答by ioreskovic

Try this one: ^[a-zA-Z,.'-]+$. :D

试试这个:^[a-zA-Z,.'-]+$。:D

Also, if you want to try out your regular expressions, rubular.comis a great place for that :D

另外,如果你想尝试你的正则表达式,rubular.com是一个很好的地方:D

You used Scanner#next, instead of Scanner#nextLine.

您使用了 Scanner#next,而不是 Scanner#nextLine。

From Scanner#next JavaDoc:

来自 Scanner#next JavaDoc:

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

从此扫描器中查找并返回下一个完整的令牌。一个完整的标记前后是与分隔符模式匹配的输入。

I believe one such delimiter is \s+:D

我相信这样的分隔符之一是\s+:D

import java.util.Scanner;

public class FirstNameParser {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String firstName = null;

        do {
            System.out.print("Please enter your FIRST name: ");
            firstName = keyboard.nextLine();
            firstName = firstName.toUpperCase();
        }

        while (!firstName.matches("^[a-zA-Z,.'-]+$"));

        System.out.println("Thanks " + firstName);

    }
}