Search code examples
javaregexasciioctal

Java - Regex to replace Octal value in string


I have set of octal values say (0177-0377). whenever these value I found in string, have to replace with ?.

    String a= "sccce¼»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕerferferfer";
    for (int i = 0177; i<= 0377 ; i++)
    {
        char x= (char) i;
        a= a.replaceAll(Character.toString(x), "?");
    }
    System.out.print(a);

but this looks good when we have small file but I have to perform this operation in 1TB file.

How we can use regex to achieve this task.


Solution

  • You can use this regex [^\p{ASCII}], which replace all non ASCII characters :

    a.replaceAll("[^\\p{ASCII}]", "?")
    

    Output :

    sccce????????????????????????????erferferfer
    

    You have to find the representation of the octal character in Hexadecimal, from the link 0177 is represent by \x7F and 0377 is represented by FF

    For your case you can use :

    a.replaceAll("[\\x7F-\\xFF]", "?")
    

    Ideone demo