简体   繁体   中英

Java regex doesn't match

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?

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:

  1. sql.replace("`", "") removes all backticks (by replacing them with an empty string) if they are already present to avoid duplicating them.
  2. \b is used for word boundary.
  3. [az]+ is for only lowercase characters.
  4. [AZ][az]+ is for a capital letter followed by lowercase letters.
  5. | is used as an or .
  6. $0 is used for the whole match argument.

Feel free to comment in case of any doubt/issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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