Regulal Expression to match on all printable characters

Posted on 2007-11-21
Last Modified: 2010-04-21
I am trying to write a regular expression to match all printable character or more accuratly discard ASCII range 32 to 126.I thought it might be easier to match on any printable character.

However i can seem to get the RegExp to work in my java app.

I am trying


but am not sure if its correct and not sure if i need to escape the \ character.

Help greatly appreciated.

My code is

public static void main(String[] args) {
          // Compile regular expression
            String patternStr =  "";
//        String patternStr = "\\w";
        Pattern pattern = Pattern.compile(patternStr);
        BufferedReader br = new BufferedReader(new InputStreamReader(;

        String inputString = null;
        try {
              inputString = br.readLine();
        } catch (IOException ioe) {
           System.out.println("Unknown Error");

        Matcher matcher = pattern.matcher(inputString);
        boolean matchFound = matcher.find();    // true
        System.out.println("Match Found " + matchFound);


Question by:Molko
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 86

Expert Comment

ID: 20326572
But is your intention to clean the string or just ignore it if it doesn't match?
LVL 86

Expert Comment

ID: 20326608
In this case it would be more efficient and probably more convenient to do something like

boolean stillValid = true;
for (int i;i < s.length() && stillValid;i++) {
	char c = s.charAt(i)
	stillValid = (c >= ' ' && c <= '~');

Open in new window


Author Comment

ID: 20326680
Yes i intend to clean the string up
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

LVL 86

Expert Comment

ID: 20326698
Even more reason then to use the above (or a variant)
StringBuilder sb = new StringBuilder(line);
boolean stillValid = true;
for (int i = sb.length() - 1;i >= 0;i--) {
	char c = sb.charAt(i)
	if (c < ' ' || c > '~') {

Open in new window


Author Comment

ID: 20326730
Ok, thank you. I've been struggling with a regular expression for ages. Thanks

Oh, is there a reason you are reading throught the string in reverse order in the 2nd example ?

LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 20326747
>>Oh, is there a reason you are reading throught the string in reverse order in the 2nd example ?

It makes altering the buffer in the loop less (un)problematic

Author Closing Comment

ID: 31410327
ok ...thanks for your help
LVL 86

Expert Comment

ID: 20326776

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question