简体   繁体   中英

how can I escape the single quote and backslash in between using regex in java using String API or any regex expresiion

How can I build a regex example that can escape single quote and backslash in a given string for example using java?

    input
  an'ish&nath$
    output
 'an\'ish&nath$'
    input
  anish\nath\
    ouput
 'anish\\nath\\'
    input
 an\'ish&nath$
    Output
 'an\\\'ish&nath$'

How can I escape the single quote and backslash in between using regex?

您必须使用\\\\\\\\'代替\\\\\\'

     System.out.println("afa'adff\\asdf".replaceAll("([\\'\\\\])", "\\\\$1"));

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