英文:
Java regex doesn't match
问题
我有这个正则表达式:
#(?<=[^\w`"\[?:]|^)[a-z_][a-z0-9_]*(?=[^\w`"(\]]|$)#Di
我正在尝试美化SQL语法。例如,从“SELECT * FROM users WHERE username=?;”变成
SELECT * FROM `users` WHERE `username`=?;
这个正则表达式匹配所有不包含反引号的单词。在匹配组中,我检查是否匹配的单词为大写。如果是,那么跳过此单词。其他单词将添加到反引号中。我的问题是,这个正则表达式没有匹配任何内容。我在PHP中有类似的代码,那里它是有效的。
function tryDelimite(string $s): string {
return preg_replace_callback('#(?<=[^\w`"\[?:]|^)[a-z_][a-z0-9_]*(?=[^\w`"(\]]|$)#Di', function (array $m): string {
return strtoupper($m[0]) === $m[0] ? $m[0] : delimite($m[0]);
}, $s);
}
function delimite(string $name): string {
return '`' . str_replace('`', '``', $name) . '`';
}
echo tryDelimite("SELECT * FROM users WHERE username=?;");
有人有想法,请吗?
英文:
I have this regex:
#(?<=[^\w`"\[?:]|^)[a-z_][a-z0-9_]*(?=[^\w`"(\]]|$)#Di
and I'm trying to make SQL syntax pretty. For example from "SELECT * FROM users WHERE username=?;" to
SELECT * FROM `users` WHERE `username`=?;
This regex matching all words, which doesn't contain backticks. And in the match group I'm checking if is matched word uppercase. If is it true, then this word is skipped. And other words are added to the backticks. My problem is, that regex doesn't match nothing. I have similar code in the PHP, and it works there.
function tryDelimite(string $s): string {
return preg_replace_callback('#(?<=[^\w`"\[?:]|^)[a-z_][a-z0-9_]*(?=[^\w`"(\]]|$)#Di', function (array $m): string {
return strtoupper($m[0]) === $m[0] ? $m[0] : delimite($m[0]);
}, $s);
}
function delimite(string $name): string {
return '`' . str_replace('`', '``', $name) . '`';
}
echo tryDelimite("SELECT * FROM users WHERE username=?;");
Does anyone have an idea, please?
答案1
得分: 0
public class Main {
public static void main(String[] args) {
// Tests
String[] sqls = {
"SELECT * FROM users WHERE username=?;",
"SELECT * FROM Users WHERE username=?;",
"SELECT * FROM `users` WHERE username=?;",
"SELECT * FROM `Users` WHERE username=?;"
};
for (String sql : sqls) {
System.out.println(sql.replace("`", "").replaceAll("(\\b[A-Z][a-z]+)|(\\b([a-z]+)\\b)", "`$0`"));
}
}
}
Output:
SELECT * FROM `users` WHERE `username`=?;
SELECT * FROM `Users` WHERE `username`=?;
SELECT * FROM `users` WHERE `username`=?;
SELECT * FROM `Users` WHERE `username`=?;
Explanation:
sql.replace("
", "")` removes all backticks (by replacing them with an empty string) if they are already present to avoid duplicating them.\b
is used for word boundary.[a-z]+
is for only lowercase characters.[A-Z][a-z]+
is for a capital letter followed by lowercase letters.|
is used as anor
.$0
is used for the whole match argument.
英文:
Do it as follows:
public class Main {
public static void main(String[] args) {
// Tests
String[] sqls = {
"SELECT * FROM users WHERE username=?;",
"SELECT * FROM Users WHERE username=?;",
"SELECT * FROM `users` WHERE username=?;",
"SELECT * FROM `Users` WHERE username=?;"
};
for (String sql : sqls) {
System.out.println(sql.replace("`", "").replaceAll("(\\b[A-Z][a-z]+)|(\\b([a-z]+)\\b)", "`$0`"));
}
}
}
Output:
SELECT * FROM `users` WHERE `username`=?;
SELECT * FROM `Users` WHERE `username`=?;
SELECT * FROM `users` WHERE `username`=?;
SELECT * FROM `Users` WHERE `username`=?;
Explanation:
- sql.replace("`", "") removes all backticks (by replacing them with an empty string) if they are already present to avoid duplicating them.
\b
is used for word boundary.[a-z]+
is for only lowercase characters.[A-Z][a-z]+
is for a capital letter followed by lowercase letters.|
is used as anor
.$0
is used for the whole match argument.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论