简体   繁体   English

正则表达式匹配除注释之外的所有内容

[英]Regex to match everything but the comment

Given this Java regex code for a PSQL statement, I'm having some issues trying to match everything but the comments in the statements.鉴于此 PSQL 语句的 Java 正则表达式代码,我在尝试匹配除语句中的注释之外的所有内容时遇到了一些问题。 This is not getting the lines with the comments in them.这不是得到带有注释的行。

^(?!.*?(?:-- [a-z]*-[a-z]*: [a-zA-z]*)).*$

This is the PSQL:这是 PSQL:

CREATE TABLE "test_table" (
   "user_id" serial PRIMARY KEY,
   "username" VARCHAR (50) UNIQUE NOT NULL, -- foreign-key: t_supply_type
   "password" VARCHAR (50) NOT NULL,
   "email" VARCHAR (355) UNIQUE NOT NULL, -- foreign-key: t_supply_type
   "created_on" TIMESTAMP NOT NULL,
   "last_login" TIMESTAMP
);

What am I missing?我错过了什么?

One simple approach for your exact data would be to just remove all occurrences of -- until the end of the line.对于您的确切数据,一种简单的方法是删除所有出现的--直到行尾。 Consider:考虑:

String line = "\"username\" VARCHAR (50) UNIQUE NOT NULL, -- foreign-key: t_supply_type";
line = line.replaceAll("\\s*--.*$", "");
System.out.println(line);

This prints:这打印:

"username" VARCHAR (50) UNIQUE NOT NULL,

Assuming you have already read the entire SQL query into a single string, the above replacement should also still work.假设您已经将整个 SQL 查询读入单个字符串,上述替换也应该仍然有效。

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

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