问题
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.
回答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.
回答2:
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\}
来源:https://stackoverflow.com/questions/23157001/how-to-replace-kate-regular-expression