遇到了麻烦正则表达式程序运行

问题描述 投票:0回答:2
import java.util.Scanner;

public class VerifySerialBayneHarris {

  public static void main(String[] args) {

    Scanner input=new Scanner(System.in);
    String text;
    String cha;


    System.out.println("Enter a Serial number: ");
    text=input.nextLine();

    if(text.matches("[^-](?!.*--)[A-Za-z0-9-]+[^-]"));{
      System.out.println("Serial number "+text+" verification \nValid");
      System.out.println("Enter a wildchar character: ");
      cha=input.nextLine();
      text= text.replaceAll("[A-Za-z0-9]", cha);
    }

    if(text.matches("[^A-Za-z0-9-]+"));
    System.out.println("Only uppercase , lowercase letters, dashes and numbers are allowed.It should have exactly 2 non-consecutive dashes in the middle. ");
  }
}

这是我到目前为止,但我不断收到这两个运行,而不是一个或另一个表达式的。一定有什么东西我失踪。提前致谢。

这里是我的准则:提示用户按照以下规则来输入序列号:

  • 只有大写和小写字母,连字符和数字是允许的。
  • 它应该在中间正好2个非连续的破折号。

你的程序应该验证该序列号是有效的,如果是的话,提示用户输入通配符。那么就应该使用野生CHAR字符屏蔽所有的字符除了破折号显示隐藏的序列号

[什么代码应该看起来像]

[1]: https://i.stack.imgur.com/xoGke.jpg
java
2个回答
1
投票

点击此处摆脱分号...

if(text.matches("[^-](?!.*--)[A-Za-z0-9-]+[^-]"));{ << HERE

...and...

if(text.matches("[^A-Za-z0-9-]+")); << HERE

您块总是执行,因为if语句由于在该空块。


0
投票

如果您在字符类有负则应该是第一:

沙到C0-yasht围巾是SH-A-TO-C0-yasht

但我不包括 - 在基团和写入图案为:[A-ZA-Z0-9] +( - [A-ZA-Z0-9] +){2}

或与POSIX:\ p {Alnum} +( - \ p {Alnum} +){2}

如果您要访问的3段:(\ p {} Alnum +) - (\ p {} Alnum) - (\ p {} Alnum)

© www.soinside.com 2019 - 2024. All rights reserved.