Can someone help me with this regular expression?

J

james.wooten1

Hello,

I would like to be able to create a regular expression that will only
return the lower ascii characters (ie: 0-127), and replace all other
characters with a carriage return. Can anyone tell me the regex for
this?

Thanks,
James
 
A

Albert

(e-mail address removed) a écrit :
Hello,

I would like to be able to create a regular expression that will only
return the lower ascii characters (ie: 0-127), and replace all other
characters with a carriage return. Can anyone tell me the regex for
this?

Thanks,
James

Why doing it with a regexp ? Do it with the (casted) int value of the
caracter.
 
M

Mark Space

Hello,

I would like to be able to create a regular expression that will only
return the lower ascii characters (ie: 0-127), and replace all other
characters with a carriage return. Can anyone tell me the regex for
this?

Thanks,
James


\p{ASCII} will match ASCII only characters. So will [\x00-\x7F]

\P{ASCII} (note upper case P) will match everything except ASCII, so
that might be what you use to match for a replace

All this information can be found here, btw:

<http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html>


String asciiOnly ( String s ) {
return s.replaceAll( "\\P{ASCII}", "\r" );
}

might do what you want (untested).
 
K

Knute Johnson

Hello,

I would like to be able to create a regular expression that will only
return the lower ascii characters (ie: 0-127), and replace all other
characters with a carriage return. Can anyone tell me the regex for
this?

Thanks,
James

public class test {
public static void main(String[] args) {
String str = "Now is the time\u00fffor all good men";

System.out.println(str.replaceAll("[^\\p{ASCII}]+","\n"));
}
}

That will be $1.00 please :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top