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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails


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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now