简体   繁体   中英

a regular expression

Now I have a String and I want to delete \\n and convert \\u to \\\\u.\u003c/i>

If the String contains \\\\u,I will not change it to \\\\\\u.\u003c/i>

I want to use String.replaceAll(), but I don`t know how to write the regular expression.

please help me. thanks.

example:

\\u =\u0026gt; \\\\u\u003c/i>

\\\\u =\u0026gt; \\\\u (do nothing)

Here's a solution using a negative look-behind. (Changes \\u\u003c/code> to \\\\u\u003c/code> only if it is not preceeded by a \\ .)

String in = "lorem ipsum \\u dolor \\\\u sit \n amet";

System.out.println(in);
System.out.println(in.replaceAll("\\n", "")
                     .replaceAll("(?<!\\\\)\\\\u", "\\\\\\\\u")); 

Prints:

lorem ipsum \u dolor \\u sit 
 amet
lorem ipsum \\u dolor \\u sit  amet
  • \\n removed
  • The first \\u\u003c/code> is changed to \\\\u\u003c/code> but while \\\\u\u003c/code> is preserved as it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM