有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在正则表达式中使用插入符号和美元?

我正在努力学习正则表达式,但我只对第一个代码感到震惊。 我读到插入符号(^)和美元($)分别用于在测试开始和结束时匹配。但我很难找出我的代码出了什么问题

  public class Test1 {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String letterA="cat will die";
            String pattern="[^cat]";
            System.out.println(letterA.matches(pattern));
            String pattern1="^(cat)";
            System.out.println(letterA.matches(pattern1));
            String letterB="Lending your cat";
            String pattern3="[cat$]";
            System.out.println(letterB.matches(pattern3));
            String pattern4="cat$";
            System.out.println(letterB.matches(pattern4));
        }

    }

每个系统都在给我输出false


共 (5) 个答案

  1. # 1 楼答案

    阅读^{上的文档。表达式必须完全匹配。例如,尝试模式.*cat.*

    此外,^中使用的[]表示否定,因此[^cat]读取正则表达式引擎——“不是c、或a或t”。不确定你是否知道,所以以防万一

  2. # 2 楼答案

    实际上,在Java中,{}方法匹配整个字符串。因此,使用^$是不必要的

    您必须使用如下内容:letterA.matches("cat.*")来匹配一个以"cat"开头的字符串

    边界匹配器:匹配由"\\b"表示的“单词边界”

    示例字符串:“卡特彼勒会死”

    letterA.matches("cat.*")//将返回true

    letterA.matches("\\bcat\\b.*")//将返回false

  3. # 3 楼答案

    插入符号和美元是Anchors,它只是告诉你的模式是否应该寻找行的开始(插入符号)和结束(美元)

    此外,在括号内,此语法也发生了更改。如果你从[^…]开始这意味着一个否定,即你试图匹配任何不在插入符号后面的字符。括号中的$告诉引擎,您正在寻找与$char匹配的字符

    此外,java中的匹配仅在匹配整个字符串时返回true

    考虑到这一点,让我们通过每一个案例,看看为什么它们不匹配:


    String letterA="cat will die";
    String pattern="[^cat]";
    

    这个正则表达式正在寻找一个不是“c”或“a”或“t”的字符。像“f”这样的字符串对于这个字符串将返回true


    System.out.println(letterA.matches(pattern));
    String pattern1="^(cat)";
    

    ()是capturing groups,它们在匹配中不起任何作用。 ^只告诉您将在字符串开头查找匹配项,而您的正则表达式正在尝试匹配字符串“cat”。对于这种情况,这是唯一可能返回true的匹配


    String letterB="Lending your cat";
    String pattern3="[cat$]";
    

    这只是试图匹配单字符字符串,这些字符串可以是“c”或“a”或“t”或“$”

    最后:

    String pattern4="cat$";
    

    只是想精确地匹配字符串“cat”(锚定到末尾,但实际上没有什么区别)

    所以,回到你的问题上来,你需要使用类似于。*匹配任意数量字符的运算符,如:

    • ^cat.*$(如果您想要一个以cat开头的字符串)
    • ^.*cat$(如果您想要一个以cat结尾的字符串)
    • ^.*cat.*$(如果您想要任何包含文本cat的字符串)
  4. # 4 楼答案

    根据java文档

    public boolean matches() Attempts to match the entire region against the pattern.

    If the match succeeds then more information can be obtained via the start, end, and group methods.

    Returns: true if, and only if, the entire region sequence matches this matcher's pattern

    既然每个人都提到了你的正则表达式的缺陷。我不会写这件事。但让我来谈谈插入符号(^)和美元($)

    阅读^$和^

    1. ^如果行有行的开头,则字面上匹配。 `实际上,说话毫无意义!为什么因为每一行都有开头,所以每一行都会匹配——即使是空的行

    请参阅下面的代码

    String letterA="";//i'm not writing anything because matches attempt to match entire region
            String pattern="^";
            System.out.println(letterA.matches(pattern));
    

    它会给你输出True

    那么每一行都有结尾吗

    String letterA="";
            String pattern="$";
            System.out.println(letterA.matches(pattern));
    

    这也会给你输出true

  5. # 5 楼答案

    也就是说,eample使用正则表达式进行部分匹配

    import java.io.Console;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    
    
    public class HelloWorld{
    
         public static void main(String []args){
            Console console = System.console();
            String letterA="cat will die";
    
                String spattern=".*cat.*";
                System.out.println(letterA.matches(spattern));
    
                Pattern pattern = 
                Pattern.compile(console.readLine("%nEnter your regex: "));
                Matcher matcher = 
                pattern.matcher(console.readLine("Enter input string to search: "));
    
                System.out.println(matcher.find());
         }
    }