简体   繁体   English

是否可以使用正则表达式消除单引号之间的单引号

[英]Will it be possible using regex to eliminate single quote in between the single quotes

There is a really big application, which is having the problem with single quotes sending to a database while executing a query, as of now using replace function to replace single quote with 2 single quotes which solves the issue for specific attributes, but I want to do a minimal change which can take care of the entire application issue in the DAL layer. 有一个非常大的应用程序,在执行查询时单引号存在发送到数据库的问题,截至目前,使用replace函数将单引号替换为2个单引号可以解决特定属性的问题,但是我想进行最小的更改,可以解决DAL层中的整个应用程序问题。

Here is my question : 这是我的问题:

EXECUTE PROCEDURE sp_procedurename ('1', 'test', 'tester's code', '0') 执行过程sp_procedurename(“ 1”,“ test”,“ tester's code”,“ 0”)

The above string should be converted to 上面的字符串应转换为

EXECUTE PROCEDURE sp_procedurename ('1', 'test', 'tester''s code', '0') Or EXECUTE PROCEDURE sp_procedurename ('1', 'test', 'testers code', '0') 执行过程sp_procedurename(“ 1”,“测试”,“测试人员的代码”,“ 0”)或执行过程sp_procedurename(“ 1”,“测试”,“测试人员的代码”,“ 0”)

either replacing with 2 single quotes or eliminating it also works. 用2个单引号引起来或将其消除也可以。

This should work. 这应该工作。

  string pattern = @"(\w+)'(\w+)";
  string replacement = "$1''$2";
  string input = "EXECUTE PROCEDURE sp_procedurename ('1', 'test', 'tester's code', '0')";
  string result = Regex.Replace(input, pattern, replacement);

Use "$1$2" as replacement if you don't want the single quotes. 如果您不希望使用单引号,请使用“ $ 1 $ 2”作为替换。

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

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