Click here to Skip to main content
14,326,529 members
Rate this:
Please Sign up or sign in to vote.
See more:
Code for pattern code result "matches = false" why cause searched is there

public class  Document{

    public static void main(String[] args) {

        String text    =
            "This is the text to be searched 45" +
            "for occurrences of the pattern.";

        
        String pattern = "searched";        

        boolean matches = Pattern.matches(pattern, text);

        System.out.println("matches = " + matches);
    }
}


What I have tried:

code refactored and searched java docs
Posted
Updated 26-Apr-19 10:21am
Comments
ZurdoDev 26-Apr-19 13:35pm
   
What?
phil.o 26-Apr-19 13:38pm
   
Obfuscated question ^^
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try:
public static void main (String[]args)
  {
    String text =
      "This is the text to be searched 45" +
      "for occurrences of the pattern.";
    String pattern = "(.*)searched(.*)";
    boolean matches = Pattern.matches (pattern, text);
    System.out.println ("matches = " + matches);
  }
   
Comments
four systems 27-Apr-19 7:58am
   
that worcs thancs
Rate this:
Please Sign up or sign in to vote.

Solution 2

Here is a link that confirms OriginalGriff's solution.
Java.lang.String.matches() in Java - GeeksforGeeks[^]

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
   
Comments
four systems 27-Apr-19 8:00am
   
cool thats so good thancs

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100