简体   繁体   English

初学者正则表达式Java

[英]Beginner Regular Expressions Java

I'm wanting to write a Regular Expression that will match to one of several variations / common uses of a name. 我想写一个正则表达式,它将与名称的几种变体/常用用法之一匹配。

I need to accept a user input, & then validate that the input matches one of the listed variations. 我需要接受用户输入,然后验证输入是否与列出的变体之一匹配。 If there is a match, I need to output that there was match, if not I should output that the input wasn't a match. 如果存在匹配项,我需要输出存在匹配项,否则,我应该输出输入不是匹配项。

This is what I have so far: 这是我到目前为止的内容:

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

public class RegExMyName {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("Name: Daimon");
        System.out.println("Common Usages: Daimon, Daimo, Dai, daimon, daimo, DAIMON");
        System.out.println("Enter any of the variations listed only: ");

        //*** HELP REQUIRED WITH PATTERN BELOW - pat
        Pattern pat = Pattern.compile();
        Matcher match = pat.matcher(scan.nextLine());


        //***HELP REQUIRED WITH FIND BELOW
        if (match.find()){
            System.out.println("*** You entered a valid name ***");
        }else{
            System.out.println("*** You entered an invalid name ***");
        }




    }
}

I would like some help constructing the pattern or receiving suggestions for alternative methods that would accomplish the same thing. 我需要一些帮助来构建模式或接收有关可以完成相同任务的替代方法的建议。

Thanks. 谢谢。

This pattern will match any of the strings Daimon , Daimo , Dai , daimon , daimo , DAIMON : 这种模式将匹配任何字符串的DaimonDaimoDaidaimondaimoDAIMON

Pattern pattern = Pattern.compile("^([dD]aimon?|DAIMON|Dai)$");

Or a slightly more generous pattern would look something like this 或稍大一些的模式看起来像这样

Pattern pattern = Pattern.compile("^dai(mon?)?$",Pattern.CASE_INSENSITIVE);

I would not use regular expression for the case you described i would rather store the possible variations of the name eg in a List would validate the input just with a simple contains. 在您描述的情况下,我不使用正则表达式,而宁愿存储名称的可能变体,例如,在List中仅使用简单的contains即可验证输入。

List<String> daimons = Arrays.asList("Daimon", "Daimo", "Dai", "daimon", "daimo", "DAIMON");

I don't see really the power of the regex for your example. 对于您的示例,我没有真正看到正则表达式的功能。 If i would not see your solution probably my would be something similar: 如果我看不到您的解决方案,则可能是类似的情况:

public class RegExMyName { 公共类RegExMyName {

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    List<String> daimons = Arrays.asList("Daimon", "Daimo", "Dai", "daimon", "daimo", "DAIMON");
    StringBuilder diamonCommonUsage = new StringBuilder();
    for(String daimonVariations : daimons) {
      diamonCommonUsage.append(daimonVariations);
      diamonCommonUsage.append(", "); //+ remove the last ","
    }        

    System.out.println("Name: Daimon");
    System.out.println("Common Usages: " + diamonCommonUsage.toString());
    System.out.println("Enter any of the variations listed only: ");

    if (match.find(daimons.contains(scan.nextLine()))){
        System.out.println("*** You entered a valid name ***");
    }else{
        System.out.println("*** You entered an invalid name ***");
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM