Java Pattern/ Matcher

前端 未结 4 995
既然无缘
既然无缘 2021-01-14 08:42

This is a sample text: \\1f\\1e\\1d\\020028. I cannot modify the input text, I am reading long string of texts from a file.


I wan

4条回答
  •  孤城傲影
    2021-01-14 08:59

    If you don't want to modify the input string, you could try something like:

    static public void main(String[] argv) {
    
                String s = "\1f\1e\1d\020028";
                Pattern regex = Pattern.compile("[\\x00-\\x1f][0-9A-Fa-f]");
                Matcher match = regex.matcher(s);
                while (match.find()) {
                        char[] c = match.group().toCharArray();
                        System.out.println(String.format("\\%d%s",c[0]+0, c[1])) ;
                }
        }
    

    Yes, it's not perfect, but you get the idea.

提交回复
热议问题