Results 1 to 4 of 4
  1. #1
    o2a1 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default need help with regex

    ok what am i doing wrong, 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

    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]")) //is this the correct code for all punctuations??
    {
    output.print("The string must consist of letters!");
    }

    }
    }



    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]

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    for searching one of those

    Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

    you can use the string "\\p{Punct}+"

  4. #4
    o2a1 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

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

    neways i put this if (s.matches("\\p{!}+")) instead of just //p and i get a few errors

    it says "Exception in thread "main" java.util.regex.PatternSyntaxException: Unknown character property name {!} near index 4
    \p{!}+ "
    ^
    Last edited by o2a1; 02-13-2011 at 01:14 AM.

Similar Threads

  1. Regex help?
    By l3ane in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 06:04 AM
  2. Using Regex
    By ron87 in forum New To Java
    Replies: 4
    Last Post: 09-12-2010, 06:26 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
  •