首页 > 解决方案 > 使用正则表达式替换列的名称

问题描述

我正在使用一个程序来重命名运行良好的表名。SPX 处理键和约束的所有事情,这非常好。我唯一的问题是我正在使用替换来更改表名,但是如果列与表名具有相同的名称并且它也在更改它,那就是代码失败的地方

这是一个例子

create Table dbo.States(
int int not null identity, 
States nvarchar(100))

因此,如果我更改StatesProvinces,它也会重命名我不想要的列,

检查我是否可以使用正则表达式跳过那里的铰孔,对于其他地方,它应该保持原样替换

我正在这样使用它

Replace(mytableString, 'States', 'provinces','all')

标签: javaregexcoldfusionlucee

解决方案


我不知道ColdFusion,但我可以建议您使用 Java 解决方案。您可以使用正则表达式States(?=\()来匹配表名,States并将其替换为所需的字符串,例如provinces. 检查以查看正则表达式的演示和解释。

Java代码:

public class Main {
    public static void main(String[] args) {
        String sql = "create Table dbo.States(\n" + 
                        "int int not null identity, \n" + 
                        "States nvarchar(100))";

        String regex = "States(?=\\()";

        String result = sql.replaceAll(regex, "provinces");

        System.out.println(result);
    }
}

输出:

create Table dbo.provinces(
int int not null identity, 
States nvarchar(100))

推荐阅读