java - 使用正则表达式替换列的名称
问题描述
我正在使用一个程序来重命名运行良好的表名。SPX 处理键和约束的所有事情,这非常好。我唯一的问题是我正在使用替换来更改表名,但是如果列与表名具有相同的名称并且它也在更改它,那就是代码失败的地方
这是一个例子
create Table dbo.States(
int int not null identity,
States nvarchar(100))
因此,如果我更改States
为Provinces
,它也会重命名我不想要的列,
检查我是否可以使用正则表达式跳过那里的铰孔,对于其他地方,它应该保持原样替换
我正在这样使用它
Replace(mytableString, 'States', 'provinces','all')
解决方案
我不知道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))
推荐阅读
- ios - 如何从 iOS 共享文件?
- typescript - 根据枚举成员区分函数和对象类型
- perl - Perl 在文件中搜索内容并使用正则表达式取出值
- python - 为什么我的 CSV 文件读作“UTF-8”但实际上是“Latin-1”
- symfony - 如何在 Twig 中使用复数形式和格式进行翻译?
- php - 嵌套循环头痛 PHP
- r - 简化基于另一个数据帧中的值对一个数据帧执行操作的函数
- google-bigquery - 如何快速将数据从 Google bigquery 迁移到 Snowflake?
- r - 视图中的错误:无法将类 'c("gg", "ggplot")' 强制转换为 data.frame
- python - 如果我在不同时间运行程序后在不同的地方得到一个 IndexError 是什么意思?