我对这个正则箱生锈有一点小问题。我需要匹配所有的‘:字’字符串,但不是在引号内。
不幸的是,像这个(?!\B"[^"]*)(:[a-zA-Z0-9]{1,})(?![^"]*"\B) (发现这里 )这样的方法不能工作,返回look-around, including look-ahead and look-behind, is not supported错误。
现在,我可以使用这个regex:(:[a-zA-Z0-9]{1,})匹配所有的':word‘字符串
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 = ?谢谢你的帮助
发布于 2020-10-07 04:33:54
在正则表达式:在引号之间不匹配单词中描述的解决方案中,您似乎可以通过匹配要替换的部分来完成这一任务,同时还可以匹配和捕获您想忽略的部分(引号中的文本)。
必需的正则表达式是('[^']*')|:\w+ -- regex101实例
这将match+capture任何包含任何其他字符的单引号,并匹配任何冒号:,后面跟着一个或多个单词字符。
然后,使用带有置换器关闭的replace_all检查捕获组:如果有捕获组,则它将是匹配的引号,因此将其替换为自身;如果没有捕获组,则为匹配关键字,因此使用所需的值替换它。
示例:
use regex::{Regex, Captures};
fn main() {
let rparams = Regex::new(r#"('[^']*')|:\w+"#).unwrap();
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, |caps: &Captures| {
match caps.get(1) {
Some(same) => same.as_str().to_string(),
None => "?".to_string()
}
}).to_string());
}备注:
[a-zA-Z0-9],但是\w (与[a-zA-Z0-9_]相同)似乎更合理,因为它允许像:num_one这样的东西。发布于 2020-10-07 03:41:52
我认为你的问题超出了正则表达式的能力。我认为最简单的声音方法是编写一个记号器,按顺序检查字符串中的所有标记,每次一个。
https://stackoverflow.com/questions/64236537
复制相似问题