How to replace Kate regular expression

半世苍凉 提交于 2019-11-30 01:50:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!