// 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#"(?ixm)(?(DEFINE)
(?<number> -? (?= [1-9]|0(?!\d) ) \d+ (\.\d+)? ([eE] [+-]? \d+)? )
(?<boolean> true | false | null )
(?<string> " ([^"\\\\]* | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* " )
(?<array> \[ (?: (?&json) (?: , (?&json) )* )? \s* \] )
(?<pair> \s* (?&string) \s* : (?&json) )
(?<object> \{ (?: (?&pair) (?: , (?&pair) )* )? \s* \} )
(?<fieldname> [a-zA-Z0-9\_\-]+ )
(?<json> \s* (?: (?&number) | (?&boolean) | (?&string) | (?&array) | (?&object) ) )
(?<fieldlist> (?: (?&fieldname) (?: , (?&fieldname) )+ )+ )
)
(?'actiontag'
\@(?&fieldname)
)
(?:\=
(?'params'
(?:
(?'match_list'(?&fieldlist))
|
(?'match_json'(?&json))
|
(?'match_string'(?:[[:alnum:]_-]+))
)
)
)?"#).unwrap();
let string = "@tag=1,2,3
@b=asdferf
@tag=a,1,b,foo_123,asf
@tag=a,b,c @A_S:-DF=foo
@ABC={\"f\":{\"A\":\"b\"}}
@A=[
{\"a\":\"b\"},
{\"C\":\"D\"}
] @B
@A={\"a\":\"b\"}
@ASDF={
\"foo\":\"bar\",
\"fdsa\":\"fdsa\"
}
@ASDF=FOO
FOO=ASD @ASDFFADS={\"ASDF\":\"fdsa\"}
@ASDF{\"asdf\":\"asdf\"}
@ASDF=\"This is a place holder\" @FDSA=\"James Brown\" @D=Fing poo
@FEE @BOO @IMAGE_TAG=FOO @IMAGE-tag=ASDF2
@ASDF={\"foo\":\"bar\"} FOO=ASDF @ASDFFADS={\"ASDF\":\"fdsa\"}
@t3={
\"glossary\": {
\"title\": \"example glossary\",
\"GlossDiv\": {
\"title\": \"S\",
\"GlossList\": {
\"GlossEntry\": {
\"ID\": \"SGML\",
\"SortAs\": \"SGML\",
\"GlossTerm\": \"Standard Generalized Markup Language\",
\"Acronym\": \"SGML\",
\"Abbrev\": \"ISO 8879:1986\",
\"GlossDef\": {
\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",
\"GlossSeeAlso\": [
\"GML\",
\"XML\"
]
},
\"GlossSee\": \"markup\"
}
}
}
}
}";
// 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/