Results 1 to 3 of 3
Thread: regex for symbols
- 08-01-2012, 02:14 AM #1Member
- Join Date
- Sep 2009
- Rep Power
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!
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 >.<
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 02:57 AM.
- 08-01-2012, 03:22 AM #2Moderator
- Join Date
- Feb 2009
- New Zealand
- Rep Power
Re: regex for symbolsI think I need to add \\ to most of these symbols
- 08-01-2012, 07:38 AM #3
Re: regex for symbols
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- By dharanikrm in forum AWT / SwingReplies: 9Last Post: 01-03-2011, 02:17 PM
- By SwissR in forum New To JavaReplies: 7Last Post: 07-27-2010, 10:07 AM
- By vivvy in forum JDBCReplies: 1Last Post: 02-17-2010, 06:17 PM
- By cowboy007 in forum Java 2DReplies: 1Last Post: 03-08-2009, 02:13 PM
- By gutters in forum New To JavaReplies: 3Last Post: 06-16-2008, 04:47 AM