0

In one of the sections in my website, users can enter a long piece of text. User may enter some machine dependent characters which are not rendered correctly, so I want to perform a validation check before they save their information.

I have a range of hex values which are machine dependent and need to be avoided. I need to check their presence and highlight those characters.

One way to solve this is to read each characters in the string and compare it with the hex range and then replace it something to highlight. Can this be done using regex in Java?

3
  • There are many answers to this question already on StackOverflow, attempt a search. Commented Jun 9, 2015 at 1:28
  • Are you talking about control codes 0x0 - 0x1F and 0x7F ? Terminal emulation esc sequences ? Commented Jun 9, 2015 at 1:32
  • I am talking about hex ranges like 0x8740 ~ 0x879F Commented Jun 9, 2015 at 1:36

1 Answer 1

3

Yes, you can.

This is a sample code to enclose the character sequences in range 0x3041 and 0x3096 by < and >.

    String s = "私は日本人です。";
    String r = s.replaceAll("[\u3041-\u3096]+", "<$0>");
    System.out.println(s);  // -> 私は日本人です。
    System.out.println(r);  // -> 私<は>日本人<です>。
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. That is just perfect. Is it possible to replace multiple different hex ranges at once using replaceAll?
@user3288346 Yes, for example "[\u3041-\u3096\u8740-\u879f]+".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.