Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace Kate regular expression

Tags:

regex

kate

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.

like image 398
Jonathan Gallagher Avatar asked Apr 18 '14 15:04

Jonathan Gallagher


People also ask

How do you substitute in regex?

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.

How to search in Kate?

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.


2 Answers

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\}
like image 155
The Guy with The Hat Avatar answered Nov 03 '22 10:11

The Guy with The Hat


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.

like image 17
Amit Joki Avatar answered Nov 03 '22 08:11

Amit Joki