Can someone help me with this regular expression?

Discussion in 'Java' started by james.wooten1, Mar 10, 2009.

  1. 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
     
    james.wooten1, Mar 10, 2009
    #1
    1. Advertisements

  2. james.wooten1

    Albert Guest

    a écrit :
    Why doing it with a regexp ? Do it with the (casted) int value of the
    caracter.
     
    Albert, Mar 10, 2009
    #2
    1. Advertisements

  3. james.wooten1

    Mark Space Guest


    \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).
     
    Mark Space, Mar 10, 2009
    #3
  4. 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 :).
     
    Knute Johnson, Mar 10, 2009
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.