Skip to content

Instantly share code, notes, and snippets.

@TheEnigmaBlade
Last active July 15, 2020 21:27
Show Gist options
  • Save TheEnigmaBlade/b7591f2dc3c0b89d7590a9617590c22a to your computer and use it in GitHub Desktop.
Save TheEnigmaBlade/b7591f2dc3c0b89d7590a9617590c22a to your computer and use it in GitHub Desktop.
Grammar regex matching Rust's std::fmt syntax (see: https://doc.rust-lang.org/std/fmt/#syntax)
(?:
// argument
([0-9]+) // 1 integer, argument
|
(\\p{Alpha}\\w*|_\\w+) // 2 identifier, argument
)?
(?:
(\\:) // 3 separator
// format_spec
// fill/align
( // 4 punctuation
// fill
.?
// align
[<>\\^]
)?
// sign
([\\+\\-])? // 5 punctuation
// '#'
(#)? // 6 punctuation
// 0
0?
// width
/// count
(?:
// parameter
// argument
((?: // 7 identifier, argument
([0-9]+) // 8 integer, argument
|
\\p{Alpha}\\w*|_\\w+
)\\$)
|
// integer
([0-9]+) // 9 integer
)?
// '.' precision
(?:
\\.
// precision
(?:
// count
(?:
// parameter
/// argument
((?: // 10 identifier, argument
([0-9]+) // 11 integer, argument
|
\\p{Alpha}\\w*|_\\w+
)\\$)
|
/// integer
([0-9]+) // 12 integer
)
|
\\*
)
)?
// type
(\\?|\\p{Alpha}\\w*|_\\w+)? // 13 identifier (maybe special)
)?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment