// include the latest version of the regex crate in your Cargo.toml
extern crate regex;
use regex::Regex;
fn main() {
let regex = Regex::new(r#"(?m)\"logUser\"([\s\:]+)([^\,]*)(\,)"#).unwrap();
let string = "Não tem dois-pontos entre a chave e o valor
(não deveria dar match, mas dá)
{ \"logUser\" 123, \"etc\" ... } <-- JSON inválido
Isso abre brechas para termos algo assim:
{ \"abc\": [\"logUser\" , 123] } <-- não deveria dar match (logUser não é chave, é parte de um array)
Também aceita vários \":\"
{ \"logUser\":::::::123, \"etc\" ... } <-- JSON inválido
---
Aceita \"nada\" entre os dois-pontos e a vírgula:
{ \"logUser\": , } <-- JSON inválido
---
E se tiver logUser no segundo nível?
{
\"abc\": 123,
\"segundoNivel\": {
\"logUser\": 123, <-- não deveria dar match, pois não está no primeiro nível
},
\"logUser\": 123, <-- deveria pegar só esse (e se desativar o flag \"g\", só iria pegar a ocorrência do segundo nível, pois ela aparece primeiro)
}
Lembre-se que um JSON não garante a ordem das chaves (é um unordered set), então ele pode ser retornado assim, dependendo das libs usadas.
---
A regex força ter uma vírgula no final, mas em um JSON as chaves não tem ordem, então deveria aceitar sem a vírgula também (vai que logUser é o último elemento):
{ \"abc\": 123, \"logUser\": \"só aceita com vírgula no final\", }
{ \"abc\": 123, \"logUser\": \"mas deveria aceitar sem vírgula\" }
";
// result will be an iterator over tuples containing the start and end indices for each match in the string
let result = regex.captures_iter(string);
for mat in result {
println!("{:?}", mat);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Rust, please visit: https://docs.rs/regex/latest/regex/