Search code examples
intellij-idea

Replacing using named capture groups in IntelliJ IDEA


I'm using IntelliJ IDEA's find and replace tool:

Screenshot of find/replace tool

My regex: <h2>(?<title>.*?)</h2>

I'd like to use my named capture group in the replace string. I know I can use $1 to refer to the first capture group, but I'd like to use its name.

I've tried to no avail:

  • $title
  • $<title>
  • $'title'
  • $"title"
  • $(title)
  • $[title]
  • $['title']
  • $["title"]
  • $("title")
  • $('title')
  • $[<title>]
  • $['<title>']
  • $["<title>"]
  • $('<title>')
  • $("<title>")
  • $<"title">

Solution

  • I stumbled on the solution to this while trying to use a named capture group in IntelliJ. I haven't been able to find documentation on it.

    The official documentation now shows how this is done (thanks @Pang):

    • Numbered capture groups use $n e.g. find: <h2>(.*?)</h2> replace: $1.
    • Named capture groups use ${<name>} e.g. find: <h2>(?<title>.*?)</h2> replace: ${title}