Results 1 to 3 of 3
Thread: Finding Regex hard to learn
- 09-21-2010, 02:29 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 18
- Rep Power
- 0
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:
All help would be greatly appreciated,Java Code:string.matches("^[ \t]+|[ \t]+$");
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).
- 09-21-2010, 03:46 AM #2
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)Note that in a Java String literal, you have to excape the backslash with another backslash.Java Code:"(^\\s.*)|(.*\\s$)"
What's wrong with the very first hit on Google -- also IMO the best resource for learning RegEx?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).
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.
dbLast edited by DarrylBurke; 09-21-2010 at 04:13 AM.
- 09-21-2010, 02:18 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 18
- Rep Power
- 0
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
-
Use hard path to jar file instead of copy
By pdfbq in forum New To JavaReplies: 5Last Post: 01-14-2012, 07:27 PM -
How hard is it.....
By neilp123 in forum New To JavaReplies: 4Last Post: 07-22-2010, 04:21 PM -
Retrieve Image From Hard Disk
By sayan751 in forum AWT / SwingReplies: 3Last Post: 02-20-2009, 03:29 PM -
Hard Token Management Framework 1.0
By Java Tip in forum Java SoftwareReplies: 0Last Post: 07-02-2008, 07:18 PM -
Extreme nooby having hard time with this app
By asterix350z in forum New To JavaReplies: 2Last Post: 12-05-2007, 07:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks