Solved

Regulal Expression to match on all printable characters

Posted on 2007-11-21
8
318 Views
Last Modified: 2010-04-21
Hi
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

^[\w\d_~`!@#$%^&*()-+=<>?/\.,;:'"][}{\|]+$

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 =  "";
       
        //^[\w\d_~`!@#$%^&*()-+=<>?/\.,;:'"][}{\|]+$
       
//        String patternStr = "\\w";
        Pattern pattern = Pattern.compile(patternStr);
     
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

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


      }

0
Comment
Question by:Molko
  • 5
  • 3
8 Comments
 
LVL 86

Expert Comment

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

Expert Comment

by:CEHJ
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

0
 

Author Comment

by:Molko
ID: 20326680
Yes i intend to clean the string up
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 86

Expert Comment

by:CEHJ
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 > '~') {
		sb.deleteCharAt(i);
	}
}

Open in new window

0
 

Author Comment

by:Molko
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 ?

0
 
LVL 86

Accepted Solution

by:
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
0
 

Author Closing Comment

by:Molko
ID: 31410327
ok ...thanks for your help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20326776
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 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