Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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">
like image 234
Adam Williams Avatar asked Jan 09 '16 20:01

Adam Williams


People also ask

How do you replace words in IntelliJ?

Press Ctrl+H or choose Edit | Find | Replace from the main menu. The search and replace pane appears on top of the active editor. If necessary, specify the search and replace options. In the search field, start typing the search string.

How do you replace special characters in regex?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")

Can I use regex in replace?

How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.


1 Answers

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.

  • 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}
like image 95
Arbiter of buffoonery Avatar answered Oct 04 '22 18:10

Arbiter of buffoonery