Results 1 to 5 of 5

Thread: Using Regex

  1. #1
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default Using Regex

    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?

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Can you provide a set of valid/invalid values?

    For example:
    Java Code:
    001a - good
    001b - good
    001f - bad
    002e - good
    ..that kind of thing.

    If you just want to check if an alphabetic character is in the string, you can use this kind of thing:
    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!");
    Full Java Regex tutorial: Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    Wikipedia on Regex: Regular expression - Wikipedia, the free encyclopedia

  3. #3
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    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.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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?

  5. #5
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

Similar Threads

  1. Please Help - Regex
    By BeeGee in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2010, 05:28 PM
  2. Help with regex
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 12:40 AM
  3. Using regex
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 03-28-2010, 11:07 PM
  4. Multiline Regex
    By zriggle in forum New To Java
    Replies: 1
    Last Post: 02-26-2009, 04:49 AM
  5. Some help with regex and loop
    By moaxjlou in forum New To Java
    Replies: 21
    Last Post: 11-02-2008, 10: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
  •