简体   繁体   中英

Java Regular expression to replace 'text with N'text'

Recently we realized that the SQL statement executed against an NVARCHAR column didn't append N before the search String.

SQL Statement is generated through some core business logic and we do not own that class. So now I need to append N before the search string in where clause.

N is used to specify a unicode string in SQL.

Example

Input String: Select * from table where key = 'text'

Output String: Select * from table where key = N'text'

I am trying some REGEX in Java where I can replace 'text' with N'text'

This is how you could do it with regex:

int start = input.start("''\w+''"); // for any character use "''.+''" and for alphanumeric use "''[\w\d]+''"
input = input.substring(0, start) + "N" + input.substring(start);

For more info on regex patterns use: https://www.rexegg.com/regex-quickstart.html

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