String finding

Recently I learned about a new way to escape strings in rust using the r##"hello world"## syntax. And it got me thinking wouldn't it be useful to be able to quickly find all the string literals in a rust file? So how would one go about doing this? Well turns out there is a tool exactly for this purpose! RegEx is a tool for finding strings in files! So please write a Reg-Ex that matches all string literals in a rust file. To keep things simple for now we ignore comments (Not that my code has any to begin with), also no chars ('k') or byte strings (b"hello"). An example is shown bellow:

let x = "a normal string starts with a \" that isn't escaped by a \\"; let y = r##"a raw string starts with a r#" and ends with "# and can contain any character yes even new lines! it is only terminated by "# followed by the same number of # as the opening delimiter"##;

Should match the following two substrings

match 1: "a normal string starts with a \" that isn't escaped by a \\" match2: "a raw string starts with a r#" and ends with "# and can contain any character yes even new lines! it is only terminated by "# followed by the same number of # as the opening delimiter"

Note that the quotes are included in the match. Also we use a single global match command meaning you don't have to worry about overlapping matches since it starts searching at the end of the last match

The longer rust file can be found here as well as a list of matches seperated by 2 new lines

Rust code Matches

Enter your Reg-Ex bellow, don't include flags. The browser uses the ECMA flavor of RegEx

Solution:




There is one crucial RegEx feature you may not know exists, the first hint only tells you of its existence:

You can match the contents of capture group 1 with \1

Need another hint? Click below:

Match both types of strings separately as they have different escape behaviors.

More hints? Click below:

Handle escape sequences separately from other characters and strings. Also matching n pairs of something

Final Hint? Click below:

Negative and positive look-arounds are your friends for checking if stuff exists, and capture groups are your friends for comparing stuff to earlier stuff