【发布时间】:2020-10-07 02:55:23
【问题描述】:
this regex crate 生锈了。我需要匹配所有 ':word' 字符串,但不在引号内。
不幸的是,像 (?!\B"[^"]*)(:[a-zA-Z0-9]{1,})(?![^"]*"\B) 这样的一些方法,发现 here,不起作用,返回 look-around, including look-ahead and look-behind, is not supported 错误。
现在,我可以使用这个正则表达式匹配所有 ':word' 字符串:(:[a-zA-Z0-9]{1,})
let rparams = Regex::new(r#"(:[a-zA-Z0-9]{1,})"#).unwrap(); // doesn't work: match the ':20'
let raw_sql = "select * from aa where a = '10-10-10 20:20'; select * from aa where a = :num";
println!("{}", rparams.replace_all(raw_sql, "?").to_string());
// Returns: select * from aa where a = '10-10-10 20?'; select * from aa where a = ?
// Expected: select * from aa where a = '10-10-10 20:20'; select * from aa where a = ?
感谢您的帮助
【问题讨论】:
-
不是 Rust,而是相关的:Regular expression: match word not between quotes
-
有什么问题?如果您有一个有效的正则表达式,那有什么问题?如果您拥有的正则表达式不起作用,它有什么问题?
-
@trentcl 问题中描述了问题:环视正则表达式不起作用,因为
regex不支持环视。他尝试了另一个正则表达式,但它匹配 所有内容 而不仅仅是引号之外的内容(请参阅“返回”与“预期”cmets)