You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/catalog/rust/rewrite-indoc-macro.md
+16-15Lines changed: 16 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,33 +12,34 @@ Previously, the same refactor is implemented by a _unreadable monster regex_ in
12
12
:::details Click to see the original regex (neovim, btw)
13
13
14
14
```vimscript
15
-
:%s/\v(indoc!|)(| )([|\{)r#"(([^#]+|\n+)+)"#/\4
15
+
:%s/\v(indoc!|)(| )([|\{)r#"(([^#]+|\n+)+)"#/`\4`
16
16
```
17
17
I have to confess that I don't understand this regex even if I use neovim, btw.
18
18
19
19
Let Claude break it down piece by piece:
20
20
21
-
-`:%s/` - This is a vim/sed substitution command
22
-
-`\v` - Very magic mode in vim, making special characters active by default
23
-
-`(indoc!|)` - Matches either "indoc!" or nothing (optional indoc!)
24
-
-`(| )([\{)` - Matches any spaces, parentheses, or curly braces
25
-
-`r#"` - Matches the literal raw string prefix in Rust
26
-
-`(([^#]+|\n+)+)` - This is the capture group that matches:
27
-
-`[^#]+` - Any characters that aren't '#'
28
-
-`|\n+` - Or one or more newlines
29
-
- The outer `()+` makes it match one or more of these sequences
30
-
-`"#` - Matches the raw string suffix
31
-
-`/\4` - Replaces the entire match with the contents of the 4th capture group
32
-
33
-
This regex appears to be designed to extract the content from within Rust raw string literals that may or may not be wrapped in the `indoc!` macro.
21
+
-`:%s/` - Vim substitution command for all lines
22
+
-`\v` - Very magic mode in vim for simpler regex syntax
23
+
-`(indoc!|)` - First capture group: matches either "indoc!" or nothing
24
+
-`(| )` - Second capture group: matches either empty string or a space
25
+
-`([|\{)` - Third capture group: matches either `[` or `{`
26
+
-`r#"` - Matches literal `r#"` (Rust raw string delimiter)
27
+
-`(([^#]+|\n+)+)` - Fourth capture group (nested):
28
+
-`[^#]+` - One or more non-# characters
29
+
-`|\n+` - OR one or more newlines
30
+
- Outer `()+` makes this repeat one or more times
31
+
-`"#` - Matches the closing raw string delimiter
32
+
-\`\4\` - Replaces with the fourth capture group wrapped in backticks
33
+
34
+
This regex is designed to find Rust raw string literals (possibly wrapped in `indoc!` macro), capture their content, and replace the entire match with just the content wrapped in backticks. It's more precise than my previous explanation and matches the pattern you're showing.
34
35
35
36
:::
36
37
37
38
<!-- Use pattern in the example. Delete this section if use YAML. -->
0 commit comments