Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: need help with regex

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default need help with regex

    my program has to output and evaluate a string,

    heres 3 examples of how it is supposed to run,

    eg.1 (no string entered)
    java Word
    Usage: java Word <string>


    eg.2(string containing punctuation and numbers gives the output "The string must consist of letters!")
    java Word AAABB@I5P
    The string must consist of letters!


    eg.3(continous character gives the output)
    java Word AAAAAAAwwwwwwwwwwwTTT
    7A11w3T


    and heres the code i got so far

    [COLOR="Blue"]import java.util.Scanner;
    import java.io.PrintStream;
     
    public class Word
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    PrintStream output = System.out;
     
    output.print("java Word ");
    String s = input.nextLine();
    if (s.equals(""))
    {
    output.print("Usage: java Word <string>");
    }
    if (s.matches("[^0-9]*[0-9]+[^0-9]*"))
    {
    output.print("The string must consist of letters!");
    }
    if (s.matches("[\\p]")) [COLOR="SeaGreen"]//is this the correct code for all punctuations??[/COLOR]
    {
    output.print("The string must consist of letters!");
    }
     
    }
    }[/COLOR]


    when i enter a number i get the correct output, however I am not sure on how to exclude punctuations from the string and i get a weird error when typing in [\\p],
    Last edited by o2a1; February 12th, 2011 at 09:49 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help with regex

    All you want is letters right? The simple letter matcher should suffice [a-zA-Z]. See Regular Expressions for more info and possible combinations

    Edit: crossposted at need help with regex. Please read The problems with crossposting
    Last edited by copeg; February 12th, 2011 at 11:52 AM.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with regex

    my bad for crossposting,, but i didnt think i would get a reply, wont happen again,
    ok so i put

    if (s.matches("[a-zA-Z]"))
    {
    output.print("The string must consist of letters!");
    }

    but if i add a number or punctuation the output "The string must consist of letters!" does not show,
    if i use a negation like (!s.matches("[a-zA-Z]") then if i input letters and number or punctuations, the output "The string must consist of letters!" always show,,
    however if i use (s.matches("[^a-zA-Z]") then if i input numbers and punctuations then i get the correct output but say if i type in "aa2" as an input then the output "The string must consist of letters!" does not show,,
    i want to exclude numbers and punctuation completely from the string but dont know how :S
    u said "The simple letter matcher should suffice [a-zA-Z]",, can u clarify this for me? this whole regex thing is confusing for me :S
    Last edited by o2a1; February 12th, 2011 at 08:23 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help with regex

    my bad for crossposting
    We just ask you be forthright and understand the problems.

    Add a quantifier to the regex, [a-zA-Z]+ will match a word of all letters (lower and uppercase) of length 1 or greater.

  5. The Following User Says Thank You to copeg For This Useful Post:

    o2a1 (February 13th, 2011)

  6. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help with regex

    ahh thank you,, got it,,
    (!s.matches("[a-zA-Z]+") did the trick,,

    hope i can get the next part done

Similar Threads

  1. [SOLVED] Java Regex Help
    By newbie in forum Java Theory & Questions
    Replies: 7
    Last Post: January 26th, 2011, 09:01 AM
  2. Need help with Regex
    By snytkine in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2010, 07:30 AM
  3. Need help with regEx
    By ptabatt in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 14th, 2010, 11:17 AM
  4. Possible regex problem?
    By Bill_H in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 25th, 2009, 01:36 AM
  5. Regex Question
    By igniteflow in forum Java SE APIs
    Replies: 1
    Last Post: August 28th, 2009, 11:46 AM