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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

734 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