1

We have set of inputs like 'java-> Way-> Project test'. ex: enter image description here

We need to eliminate control characters only. we used [\p{C}] for matching those characters unfortunately it matches the the tab space and new line characters. Kindly give(Valid regex pattern) solutions to solve this issue.

1 Answers1

2

In Java you can use this regex:

[\p{Cntrl}&&[^\p{Space}]]

This will match a control character i.e. [\x00-\x1F\x7F] except a whitespace character i.e. [ \t\n\x0B\f\r]

Java RegEx Reference

In Java use:

String re = "[\\p{Cntrl}&&[^\\p{Space}]]";

Example:

String title = "xyz a\u0000b\u0007c\u008fd\u0009 - foo \u0009 bar";
title = title.replaceAll("[\\p{Cntrl}&&[^\\p{Space}]]", "");
System.out.printf("[%s]%n", title);
//=> [xyz abcd   - foo   bar]
anubhava
  • 761,203
  • 64
  • 569
  • 643