Results 1 to 3 of 3
  1. #1
    wildheart25c is offline Member
    Join Date
    Sep 2009
    Rep Power

    Default regex for symbols

    I have a string which contains letters, numbers, and symbols. I'd like to check the string to make sure it doesn't contain any symbols. One solution is to pass it through a for loop and use the charAt method. I don't want to do that. I'd like to use regular expressions!
    Java Code:
    String input = "I have a cat, but I like my dog better.";
    Pattern p = Pattern.compile("(mouse|cat|dog|wolf|bear|human)");
    Matcher m = p.matcher(input);
    while (m.find())
     System.out.println("Found a " + + ".");
    This example, although good for comparing numbers and letters, it's not good when comparing symbols. Now when I say symbols, I'm referring to every character on your keyboard:

    If I replace this Pattern p = Pattern.compile("(mouse|cat|dog|wolf|bear|human)") ; with this Pattern p = Pattern.compile("(%|&"); and run the app, IT WILL ALWAYS PRINT FOUND. Why is that? Is their a fix around it? and it HAS TO BE the way I need it to be. The symbols must be in the regex. I'm aware that there is an alphanumeric method that I can use which simplifies things for me but I need it to be this way. I might decide to use a different language for strings. I might even want to include subscript numbers for example. I'm aware that I'll need to add utf-8 as a parameter to the bufferreader when i create its object.

    So is there a solution? Or do I have to use the charAt method? It seems so "old" compared to regex >.<

    Thank you.

    EDIT: what im trying to say is that if i put numbers or letters between (" ") in the Pattern line, java accepts it. But if I put symbols in there, java won't recognize it at all. Wat do i do?

    EDIT EDIT: I think i fixed it. I think I need to add \\ to most of these symbols as they have a meaning in regular expressions. Like ^ means ANYTHING OTHER THAN what comes after it so i'll need to add \\ to ^. Will keep you updated. But if this isn't the solution, or if there's a better solution, feel free to answer :D
    Last edited by wildheart25c; 08-01-2012 at 01:57 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: regex for symbols

    I think I need to add \\ to most of these symbols
    Yes, and don't forget that \ itself is interpreted in a special way within a Java string literal. So, for instance, "\\\\^" is a string with three characters in it: two \ and a ^.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: regex for symbols

    Quote Originally Posted by wildheart25c View Post
    Like ^ means ANYTHING OTHER THAN what comes after
    Nope. Better learn systematically before your wild guesses take root. - Regex Tutorial, Examples and Reference - Regexp Patterns
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    Pattern (Java Platform SE 6)

    The caret (^) signifies the start of input; or as the first character is a character class, a negation.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. paste special symbols in JTextField
    By dharanikrm in forum AWT / Swing
    Replies: 9
    Last Post: 01-03-2011, 01:17 PM
  2. Checking If A String Contains Symbols
    By SwissR in forum New To Java
    Replies: 7
    Last Post: 07-27-2010, 09:07 AM
  3. Representing ERD with symbols
    By vivvy in forum JDBC
    Replies: 1
    Last Post: 02-17-2010, 05:17 PM
  4. Plotting Symbols in Java 2D
    By cowboy007 in forum Java 2D
    Replies: 1
    Last Post: 03-08-2009, 01:13 PM
  5. How to cut symbols from a string?
    By gutters in forum New To Java
    Replies: 3
    Last Post: 06-16-2008, 03:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts