在java中使用regex获取名称作为输入

时间:2022-10-01 08:54:55

I am a beginner in both, Java and regular expressions. I want to get a name as an input, by which I mean that only names that have English alphabets A-Z, case insensitive and spaces.

我是Java和正则表达式的初学者。我想获得一个名称作为输入,我的意思是,只有具有英文字母a - z、不区分大小写和空格的名称。

I am using a Scanner class to get my input but my code doesn't work. It looks like:

我正在使用一个扫描器类来获取输入,但是我的代码不能工作。它看起来像:

Scanner sc= new Scanner(System.in);
String n;

while(!sc.hasNext("^[a-zA-Z ]*$"))
{
    System.out.println("That's not a name!");
    sc.nextLine();
}
n = sc.next();

I checked my regular expression on the website regex101.com and found out that it works fine.

我在regex101.com网站上查看了我的正则表达式,发现它工作得很好。

For example, If I input it my name, Akshay Arora , the regex site says it is okay but my program prints

例如,如果我输入我的名字,Akshay Arora, regex网站说它没问题,但是我的程序打印。

That's not a name
That's not a name

Same line is printed twice and it again asks me for input. Where am I going wrong?

同样的行被打印了两次,它再次要求我输入。我哪里出错了?

2 个解决方案

#1


2  

Two parts are wrong:

两个部分是错误的:

  • $ and ^ anchors are considered in the context of entire input, not in the context of the next token. It will never match, unless the input has a single line that matches the pattern in its entirety.
  • 和^锚被认为美元在整个输入,没有下一个记号的上下文中。它永远不会匹配,除非输入只有一条线与模式完全匹配。
  • You use default delimiters, which include spaces; therefore, Scanner will never return a token with a space in it.
  • 使用默认的分隔符,包括空格;因此,扫描器永远不会返回带有空格的令牌。

Here is how you can fix this:

以下是解决这个问题的方法:

Scanner sc = new Scanner(System.in);
sc.useDelimiter("\n");
String n;

while(!sc.hasNext("[a-zA-Z ]+"))
{
    System.out.println("That's not a name!");
    sc.nextLine();
}
n = sc.next();

Demo.

演示。

#2


1  

Here its sample program related to regex.

这里是与regex相关的示例程序。

public class Program {

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    String inputName = sc.next();

    String regex = "^[a-zA-Z ]*$";
    // Compile this pattern.
    Pattern pattern = Pattern.compile(regex);

    // See if this String matches.
    Matcher m = pattern.matcher(inputName);
    if (m.matches()) {
        System.out.println("Valid Name");
    } else
        System.out.println("Invalid Name");

    }
}

Hope this will help you

希望这能对你有所帮助

#1


2  

Two parts are wrong:

两个部分是错误的:

  • $ and ^ anchors are considered in the context of entire input, not in the context of the next token. It will never match, unless the input has a single line that matches the pattern in its entirety.
  • 和^锚被认为美元在整个输入,没有下一个记号的上下文中。它永远不会匹配,除非输入只有一条线与模式完全匹配。
  • You use default delimiters, which include spaces; therefore, Scanner will never return a token with a space in it.
  • 使用默认的分隔符,包括空格;因此,扫描器永远不会返回带有空格的令牌。

Here is how you can fix this:

以下是解决这个问题的方法:

Scanner sc = new Scanner(System.in);
sc.useDelimiter("\n");
String n;

while(!sc.hasNext("[a-zA-Z ]+"))
{
    System.out.println("That's not a name!");
    sc.nextLine();
}
n = sc.next();

Demo.

演示。

#2


1  

Here its sample program related to regex.

这里是与regex相关的示例程序。

public class Program {

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    String inputName = sc.next();

    String regex = "^[a-zA-Z ]*$";
    // Compile this pattern.
    Pattern pattern = Pattern.compile(regex);

    // See if this String matches.
    Matcher m = pattern.matcher(inputName);
    if (m.matches()) {
        System.out.println("Valid Name");
    } else
        System.out.println("Invalid Name");

    }
}

Hope this will help you

希望这能对你有所帮助