简体   繁体   English

Java 正则表达式替换'text with N'text'

[英]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.最近我们意识到,针对 NVARCHAR 列执行的 SQL 语句在搜索字符串之前没有 append N。

SQL Statement is generated through some core business logic and we do not own that class. SQL 语句是通过一些核心业务逻辑生成的,我们不拥有该 class。 So now I need to append N before the search string in where clause.所以现在我需要 append N在 where 子句中的搜索字符串之前。

N is used to specify a unicode string in SQL. N 用于指定 SQL 中的 unicode 字符串。

Example例子

Input String: Select * from table where key = 'text'输入字符串: Select * from table where key = 'text'

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

I am trying some REGEX in Java where I can replace 'text' with N'text'我正在 Java 中尝试一些正则表达式,我可以用 N'text' 替换'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有关正则表达式模式的更多信息,请使用: https://www.rexegg.com/regex-quickstart.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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