Java remove template from string using regex

I need to clear a string from the following substrings:

\n

\uXXXX ( X is a number or symbol)

eg. "OR\n\nThe Central Site Engineering\u2019s \u201cfrontend\u201d, where developers turn to"

-> "OR The Central Site Engineering frontend , where developers turn to"
I tried using the String replaceAll method, but dnt knows how to overcome the \ uXXXX problem, and also did not work for \ n

 String s = "\\n"; data=data.replaceAll(s," "); 

what does this regular expression look like in java?

thanks for the help

+4
source share
2 answers

Problem with string.replaceAll("\\n", " "); that is, replaceAll expects a regular expression, and \ in a regular expression is a special character used, for example, to create character classes, such as \d , which represents numbers, or to call special characters on regular expressions, such as + .

So, if you want to combine \ in a Javas regex, you need to escape twice:

  • once in regex \\
  • and once in String "\\\\" .

like replaceAll("\\\\n"," ") .

You can also let the regex engine do the escaping for you and use the replace method, for example

replace("\\n"," ")

Now, to remove \uXXXX , we can use

replaceAll("\\\\u[0-9a-fA-F]{4}","")


Also remember that Lines are immutable, so each call to str.replace.. does not affect the value of str , but creates a new line. Therefore, if you want to keep this new line in str , you will need to use

 str = str.replace(..) 

So your solution may look like

 String text = "\"OR\\n\\nThe Central Site Engineering\\u2019s \\u201cfrontend\\u201d, where developers turn to\""; text = text.replaceAll("(\\\\n)+"," ") .replaceAll("\\\\u[0-9A-Ha-h]{4}", ""); 
+5
source

This is best done in 2 parts, I think:

 String ex = "OR\n\nThe Central Site Engineering\u2019s \u201cfrontend\u201d, where developers turn to"; String part1 = ex.replaceAll("\\\\n"," "); // The firs \\ replaces the backslah, \n replaces the n. String part2 = part1.replaceAll("u\\d\\d\\d\\d",""); System.out.println(part2); 

Try =)

0
source

All Articles