【发布时间】:2022-09-27 16:04:17
【问题描述】:
我在使用 Regex 获取 SQL 查询中的所有参数列表时遇到问题。
查询示例:
SELECT ... WHERE col1 = :user AND col2 = \'HELLO\' OR col3 = :language
要获取参数,我使用以下正则表达式模式:
Pattern.compile(\":([\\\\w.$]+|\\\"[^\\\"]+\\\"|\'[^\']+\')\", Pattern.MULTILINE)
该模式正确返回参数列表:
:user
:language
问题在于另一种类型的查询,其中文字可能包含字符 \':\'
WHERE col1 = :user AND some_date > \'2022-09-26T10:22:55\'
这种情况下的参数列表是:
:user
:22
:55
有没有更好的方法不将文字内容视为参数?
-
Pattern.compile(\"[^\'\\\":]*:([\\\\w.$]+|\\\"[^\\\"]+\\\"|\'[^\']+\')\", Pattern.MULTILINE) -
感谢您的回复。但在场景中:
code <> \'*\'\\nAND code IN (select x from ... where user = :user)它认为\\nAND code IN (select x from ... where user = :user是参数。 -
Pattern.DOT_ALL将让.也匹配换行符。忘记了