Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Regex definition unclear...

  1. #1
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Regex definition unclear...

    I've read the documentation. It's still unclear. I've searched this forum. No luck. So would someone kindly explain in simple terms why the asterisk ("zero or any occurrences") is necessary - as described in the comments above the expression. Just guessing: the asterisk is the "thereafter" part? But how do we arrive at that from the "zero or any occurrences" described in the Pattern documentation?

    Here's the example:

     
    // This method verifies that firstName starts with an uppercase
    // letter and contains lowercase letters thereafter.
     
    public void setFirstName(String first)
    {
        if (firstName.matches("[A-Z][a-z]*"))
        {
             this.firstName = first;
        } 
        // and blah blah blah...

    After reading the documentation, it seems to me that the expression would say: "the first letter of firstName must be uppercase and the second letter is must be lowercase, zero or any number of times" (which of course doesn't make sense). The comments above tell me how the expression should be read, but I don't see the logic in that translation --- yet.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Regex definition unclear...

    The regular expression has 3 parts:
    • The first part is [A-Z] which means that the first letter must be present and must be capitalized.
    • The next part is [a-z] which will match any lower-case letter.
    • The third part is the asterisk, *, which tells the regex engine that the preceding character (the lower case letter) may be repeated 0 or more times. The key is the asterisk affects the character that comes before it.


    So this means that valid Strings include A, Hello, John, Xasdflaksdfajse, and Phillip. Non-valid Strings would be ones with any non-letter characters, any that don't start with an upper case or any with uppercase letters that are not in the first position.
    Last edited by curmudgeon; August 27th, 2012 at 02:59 PM.

  3. #3
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Regex definition unclear...

    curmudgeon,

    Thanks for a great explanation!

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Regex definition unclear...

    You're quite welcome!

Similar Threads

  1. need help with regex
    By o2a1 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2011, 07:50 AM
  2. the definition of a wrapper class
    By nickypass in forum Java Theory & Questions
    Replies: 1
    Last Post: October 17th, 2010, 01:54 AM
  3. Need help with Regex
    By snytkine in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2010, 07:30 AM
  4. Need help with regEx
    By ptabatt in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 14th, 2010, 11:17 AM
  5. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM

Tags for this Thread