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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 48
servlet filter example 37 63
maven module vs maven project 3 23
How to determine if a string is a valid SHA value 7 33
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

773 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