Results 1 to 5 of 5
Thread: Using Regex
- 09-12-2010, 06:12 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 52
- Rep Power
- 0
- 09-12-2010, 06:18 AM #2
Can you provide a set of valid/invalid values?
For example:
..that kind of thing.Java Code:001a - good 001b - good 001f - bad 002e - good
If you just want to check if an alphabetic character is in the string, you can use this kind of thing:
Full Java Regex tutorial: Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)Java Code:String s = "001a"; // Can be whatever string you want here... Pattern p = Pattern.compile("[a-z]+"); if (p.matcher(s).find()) System.out.println("Found!"); else System.out.println("Did not find!");
Wikipedia on Regex: Regular expression - Wikipedia, the free encyclopedia
- 09-12-2010, 06:21 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 52
- Rep Power
- 0
it can be any string like 002d
I tried it like below
public static void main(String [] args) {
String s = "001d";
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(s);
if(matcher.matches()){
System.out.println(s);
}
}
but above code never printed 's' coz if condition was always false.
So i was confused.
- 09-12-2010, 06:25 AM #4
The .matches() attempts to match the pattern with the ENTIRE string. That means that only single characters will match your pattern.
However, if you use .find(), it will find it anywhere in the string.
Is that what you're looking for?
- 09-12-2010, 06:26 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 52
- Rep Power
- 0
Similar Threads
-
Please Help - Regex
By BeeGee in forum Advanced JavaReplies: 0Last Post: 04-28-2010, 05:28 PM -
Help with regex
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 03-29-2010, 12:40 AM -
Using regex
By SteroidalPsycho in forum New To JavaReplies: 0Last Post: 03-28-2010, 11:07 PM -
Multiline Regex
By zriggle in forum New To JavaReplies: 1Last Post: 02-26-2009, 04:49 AM -
Some help with regex and loop
By moaxjlou in forum New To JavaReplies: 21Last Post: 11-02-2008, 10:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks