Results 1 to 6 of 6
  1. #1
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default How to use Regex?

    I've seen on a Java tutorial website how to use regex but it always returns false even when it is true.

    I've done a simplified version of my code to make it easier to assist me.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class test {
    	static String text = "sdijv";
    	
    	public static void main (String[] args) {
    		JOptionPane.showMessageDialog(null, "" + text.matches("^[a-z]$"));
    		
    	}
    }
    Newb today, Pro tomorrow,
    Soulmed

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default

    What do you think that regular expression is "saying"? Why do you think that should match?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    to my understanding, that regex should validate that all characters in the string are letters between a to z and nothing else.
    Newb today, Pro tomorrow,
    Soulmed

  4. #4
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    I found the problem... I didn't put in the brakets beside my letters. The regex should go like follow: ^[a-z]{1,}$
    Newb today, Pro tomorrow,
    Soulmed

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

    Default

    'One or more times' is more conventionally written as '+'
    Java Code:
    ^[a-z]+$
    Also, since you want to match repetition of a single character class (using matches -- not find) to the entire String, including the start and end of input are redundant.
    Java Code:
    [a-z]+
    db

    edit There's also a posix character class \p{Lower} which is equivalent to [a-z]. Read the documentation for Pattern.
    Last edited by DarrylBurke; 04-28-2011 at 04:39 PM.

  6. #6
    soulmed is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    thank you for the advice.
    Newb today, Pro tomorrow,
    Soulmed

Similar Threads

  1. RegEx
    By watle in forum New To Java
    Replies: 0
    Last Post: 03-22-2011, 08:07 AM
  2. need help with regex
    By o2a1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2011, 12:53 AM
  3. Please Help - Regex
    By BeeGee in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2010, 05:28 PM
  4. Help with regex
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 12:40 AM
  5. Using regex
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 03-28-2010, 11:07 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
  •