Results 1 to 3 of 3
Thread: regex for symbols
- 08-01-2012, 01:14 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 13
- Rep Power
- 0
regex for symbols
Hello,
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!
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: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 " + m.group() + ".");
`~!@#$%^&*()-_=+[{]};:'"\|/?.>,<
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 :DLast edited by wildheart25c; 08-01-2012 at 01:57 AM.
- 08-01-2012, 02:22 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: regex for 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 ^.I think I need to add \\ to most of these symbols
- 08-01-2012, 06:38 AM #3
Re: regex for symbols
Nope. Better learn systematically before your wild guesses take root.
Regular-Expressions.info - 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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
paste special symbols in JTextField
By dharanikrm in forum AWT / SwingReplies: 9Last Post: 01-03-2011, 01:17 PM -
Checking If A String Contains Symbols
By SwissR in forum New To JavaReplies: 7Last Post: 07-27-2010, 09:07 AM -
Representing ERD with symbols
By vivvy in forum JDBCReplies: 1Last Post: 02-17-2010, 05:17 PM -
Plotting Symbols in Java 2D
By cowboy007 in forum Java 2DReplies: 1Last Post: 03-08-2009, 01:13 PM -
How to cut symbols from a string?
By gutters in forum New To JavaReplies: 3Last Post: 06-16-2008, 03:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks