Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm programming in Java and I want to use regex to look for any data with a percent sign, e.g. 10%, 20%,30%,etc. I've tried a.matches("%")but that only returns true if the string matches exactly the percent sign. What should be the correct regular expression to use? Thanks

share|improve this question

closed as off topic by Brad Patton, Sathya Apr 26 '13 at 2:46

Questions on Super User are expected to relate to computer software or computer hardware within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

that gave me an error but a.matches("%*") works! thanks! – pew007 Apr 26 '13 at 2:28
This guide appears to be better than my suggestion. In java the wildcard is actually . apparently (I know little of Java). – nerdwaller Apr 26 '13 at 2:31

If you're just looking for a '%', then String.indexOf('%') would probably be faster and simpler than any regular-expression-based solution.

Considering regular expressions, the matches() function attempts to match the entire string to the pattern, as you've discovered. Matcher.find() will look for substrings within the string that match the pattern, for example:

Pattern pp = Pattern.compile("%");
Matcher mm = Pattern.matcher(someString);
if (mm.find()) {
    // someString contains a '%'
share|improve this answer

Not the answer you're looking for? Browse other questions tagged .