Results 1 to 3 of 3
  1. #1
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default Finding Regex hard to learn

    Hey guys,
    I'm having a bit of trouble learning regex. I understand that the string.matches() method needs to match the whole string for it to return true in java.

    At the moment I have this to check if there is what space at either the beginning or the end of the string. This works with the replace method but is missing something to ever return true with the matches() method:
    Java Code:
    string.matches("^[ \t]+|[ \t]+$");
    All help would be greatly appreciated,
    Thanks,
    Dan

    P.S. are there any really good resources that can teach regex I have tried google but it doesn't seem to sink in (also I am learning it in flash at the same time).

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Quote Originally Posted by Dan0100 View Post
    Hey guys,
    I'm having a bit of trouble learning regex. I understand that the string.matches() method needs to match the whole string for it to return true in java.

    At the moment I have this to check if there is what space at either the beginning or the end of the string. This works with the replace method but is missing something to ever return true with the matches() method:
    Java Code:
    string.matches("^[ \t]+|[ \t]+$");
    The metacharacter of whitespace (presuming that's what you mean by "what space") is \s and you want to test for whitespace EITHER at the beginning OR at the end of the input. I'll presume that OR isn't an EXOR i.e. that a string with whitespace at both beginning AND end should also match.

    So in plain English: match
    the beginning of the input followed by whitespace followed by anything
    or
    anything followed by whitespace followed by the end of input

    (untested -- test and report back)
    Java Code:
    "(^\\s.*)|(.*\\s$)"
    Note that in a Java String literal, you have to excape the backslash with another backslash.


    P.S. are there any really good resources that can teach regex I have tried google but it doesn't seem to sink in (also I am learning it in flash at the same time).
    What's wrong with the very first hit on Google -- also IMO the best resource for learning RegEx?
    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
    But make sure you go through the documentation for Pattern so you don't miss out on the Java-specific constructs.

    db
    Last edited by DarrylBurke; 09-21-2010 at 04:13 AM.

  3. #3
    Dan0100 is offline Member
    Join Date
    Aug 2010
    Posts
    18
    Rep Power
    0

    Default

    Thank you so much Darryl.Burke. Your regex worked perfectly.
    O.K. so I guess it is just a case of me knuckling down and studying Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns.

    Thank you again,
    Dan

Similar Threads

  1. Use hard path to jar file instead of copy
    By pdfbq in forum New To Java
    Replies: 5
    Last Post: 01-14-2012, 07:27 PM
  2. How hard is it.....
    By neilp123 in forum New To Java
    Replies: 4
    Last Post: 07-22-2010, 04:21 PM
  3. Retrieve Image From Hard Disk
    By sayan751 in forum AWT / Swing
    Replies: 3
    Last Post: 02-20-2009, 03:29 PM
  4. Hard Token Management Framework 1.0
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-02-2008, 07:18 PM
  5. Extreme nooby having hard time with this app
    By asterix350z in forum New To Java
    Replies: 2
    Last Post: 12-05-2007, 07:24 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
  •