I am using the Kate editor. Here is a minimal example that shows my problem:
I have a file with a bunch of occurrences of:
\command{stuff}
where stuff is some arbitrary string of letters. I want to replace this with
\disobey{stuff}
where stuff is unchanged. The regular expression:
\\command\{[a-zA-Z]*\}
matches such expressions. So I pull the replace dialog with CTRL-r, and enter
Find: \\command\{[a-zA-Z]*\}
Replace: \\disobey\{\1\}
So in the document, an actual instance is say,
\command{exchange}
and when I hit the replace button is changed to
\disobey{1}
In the Kate documentation: Appendix B: Regular Expressions, \1 should match the first pattern used. Is this indeed the correct syntax? I have also tried $1, #1, and various other things.
To perform a substitution, you use the Replace method of the Regex class, instead of the Match method that we've seen in earlier articles. This method is similar to Match, except that it includes an extra string parameter to receive the replacement value.
Select Open Files to search all files currently open in Kate. Select Folder to search inside a folder and optionally its subfolders. Select Current File to search only in the active file. If the Projects plugin is loaded, you can also search in the Current Project or in All Open Projects.
Here is a quote directly from the documentation:
The string
\1
references the first sub pattern enclosed in parentheses
So you need to put [a-zA-Z]*
in a capturing group, like ([a-zA-Z]*)
.
Find: \\command\{([a-zA-Z]*)\}
Replace: \\disobey\{\1\}
Wrap the value with ( )
to capture it as a group, so you can use it in your replace
So change your find regex like this:
\\command\{([a-zA-Z]*)\}
and you should do fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With