Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2015
    Location
    North Pole
    Posts
    3
    Rep Power
    0

    Default Question about String matches() Method

    I was looking at the String matches() Method page on Tutorialspoint and I saw that it had some things that I didn't understand in it.

    Here is the example that was shown.
    Java Code:
    import java.io.*;
    
    public class Test{
       public static void main(String args[]){
          String Str = new String("Welcome to Tutorialspoint.com");
    
          System.out.print("Return Value :" );
          System.out.println(Str.matches("(.*)Tutorials(.*)"));
    
          System.out.print("Return Value :" );
          System.out.println(Str.matches("Tutorials"));
    
          System.out.print("Return Value :" );
          System.out.println(Str.matches("Welcome(.*)"));
       }
    }
    On lines 8 and 14, it has (.*). What is this, and what does it do?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Question about String matches() Method

    In Java regular expressions, . matches any character. * is a multiplier which says 0 or more. So .* says, match 0 or more characters. Regular expression engines assign special meanings to characters which comprise regular expression patterns. To see what Java supports check out the Pattern class in the Java JDK.

    Regards,
    Jim
    Last edited by jim829; 09-07-2015 at 11:07 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Question about String matches() Method

    In your last thread, Norm guided you to a regex tutorial. Clearly, you have ignored that guidance.

    You absolutely cannot learn regexes one metacharacter at a time. Here's another tutorial, which I found very helpful: Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

    Also refer frequently to the documentation for java.util.regex.Pattern

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 06-04-2014, 10:23 PM
  2. What Regex matches this String?
    By AnouarAbdelbari in forum New To Java
    Replies: 3
    Last Post: 09-30-2013, 01:33 PM
  3. Replies: 6
    Last Post: 06-21-2013, 03:42 PM
  4. Replies: 10
    Last Post: 12-07-2010, 12:29 PM
  5. Replies: 0
    Last Post: 11-17-2009, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •