I am new to Java and i want to use regex class for pattern matching..
I am processing string like 003b , 002v
I want to check if any character is present(from a to z or A to Z) in the above string using regex.
Can some one help me?
Printable View
I am new to Java and i want to use regex class for pattern matching..
I am processing string like 003b , 002v
I want to check if any character is present(from a to z or A to Z) in the above string using regex.
Can some one help me?
Can you provide a set of valid/invalid values?
For example:
..that kind of thing.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)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
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.
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?
Yes mate.. i got it now..
thanks for ur help
Cheers,:)