Posted on ::

I rarely use regex (regular expressions) beyond basics such as these:

  • match one or more characters: .+
  • match beginning of line: ^
  • match end of line: $

I recently had to do more fancy things:

  • exclude a character from matching: [^x]

  • allow . to match \n: (?s:.)

  • do multi-line matching: (?m:\s)

For an example of how 2 of the above works, consider this text:

asm!(
    "jmp {}",
    label {},
);

The following regex will match asm!, followed by label, followed by {, while ignoring all that is between those:

asm!(?s:.)+label(?m:\s)*\{

The regex dialect used is that of the Rust regex crate.