21

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.

Jonathan Gallagher
  • 2,115
  • 2
  • 17
  • 31
  • In the exact same documentation, it says that `\1` matches "the first **sub** pattern **enclosed in parentheses**" – The Guy with The Hat Apr 18 '14 at 15:49
  • Does this still work as advertised? I'm using Kate 23.04. I'm trying to clean up columns of financial data copied from PDF. Mode: Reg Exp; FIND: `\d(\s)\d`; REPLACE: `\1 ,` so I can add coma to convert to CSV. But the capture group is ignored and the whole find match is replaced. – xtian Aug 02 '23 at 19:02

2 Answers2

27

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\}
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
  • To be honest, that bit of documentation is confusing. In appendix B, which I guess you are referencing, they first discuss patterns. Then they discuss sub patterns. The sub is supposed to refer to "substitution". I honestly thought it meant subpattern as in ab < abc. So I was like, oh that's strange, you can specify subpatterns with parentheses. They should have called them substitutions or substitution patterns imo. – Jonathan Gallagher Apr 19 '14 at 19:18
17

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.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95