为什么我的代码不查找非字母字符?(正则表达式)

huangapple go评论59阅读模式
英文:

Why isn't my code looking for non-letters? (Regular expressions)

问题

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

class Main { 
  public static void main(String[] args) {
    String naming;

    Scanner input = new Scanner(System.in);
    System.out.print("What is your name: ");
    naming = input.nextLine();  
    input.close();

    //**
    Pattern pattern = Pattern.compile("[^a-zA-Z]"); // Updated pattern to match non-letters
    Matcher matcher = pattern.matcher(naming); // Use the user's input for matching
    boolean check = matcher.find();
    if (check) {
      System.out.println("Invalid name entered");
      //**
    } else {
      System.out.print("continue");
    }
  }
}

** 之间的部分是我卡住的部分。这个程序应该要求输入你的名字,如果你输入任何不是字母的字符,它会显示"Invalid name entered"。但它只会显示"continue"。我做错了什么?此外,我想在System.out.println("Invalid name entered");行下使用break,但repl.it告诉我"在循环或开关之外不能使用break"。顺便说一下,我必须在这里使用正则表达式。

我不确定该怎么办。Pattern pattern = Pattern.compile(naming); 应该将名称设置为模式,然后 Matcher matcher = pattern.matcher("[^a-zA-Z]"); 应该查找非字母字符。最后,

boolean check = matcher.find();
if (check) {
   System.out.println("Invalid name entered");
} else {
   System.out.print("continue");
}

应该在找到非字母字符时打印"Invalid name entered",但它只是忽略了这一点,而是打印"continue"。

英文:
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Main { 
  public static void main(String[] args) {
    String naming;

    Scanner input = new Scanner(System.in);
    System.out.print("What is your name: ");
    naming = input.nextLine();  
    input.close();

    //**
    Pattern pattern = Pattern.compile(naming);
    Matcher matcher = pattern.matcher("[^a-zA-Z]");
    boolean check = matcher.find();
    if (check) {
      System.out.println("Invalid name entered");
      //**
    } else {
      System.out.print("continue");
    }
  }
}

Between the ** is the part I'm stuck on. This program is supposed to ask for your name, and if you enter anything that's not a letter, then it will say "Invalid name entered." But instead it just says "continue". What am I doing wrong? Also, I want to make the program break under the System.out.println("Invalid name entered"); line, but repl.it tells me "break cannot be used outside of a loop or switch". I have to use regular expressions here by the way.

I'm not sure what to do. Pattern pattern = Pattern.compile(naming); is supposed to set the name to a pattern, then Matcher matcher = pattern.matcher("[^a-zA-Z]"); is supposed to look for non-letters. Finally,

boolean check = matcher.find();
   if (check) {
      System.out.println("Invalid name entered");
   } else {
      System.out.print("continue");

is supposed to print "Invalid name entered" if non-letters are found, but it just ignores that and prints "continue" instead.

答案1

得分: 0

你的正则表达式和被搜索的字符串的顺序是错误的。而不是

Pattern pattern = Pattern.compile(naming);
Matcher matcher = pattern.matcher("^[a-zA-Z]");

你需要像这样:

Pattern pattern = Pattern.compile("^[a-zA-Z]");
Matcher matcher = pattern.matcher(naming);
英文:

You've got the regular expression and the string that's being searched the wrong way round. Instead of

Pattern pattern = Pattern.compile(naming);
Matcher matcher = pattern.matcher("[^a-zA-Z]");

You need something like

Pattern pattern = Pattern.compile("[^a-zA-Z]");
Matcher matcher = pattern.matcher(naming);

huangapple
  • 本文由 发表于 2023年2月10日 11:33:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定