Find and replace text using regular expressions
When you want to search and replace specific patterns of text, use regular expressions. They can help you in pattern matching, parsing, filtering of results, and so on. Once you learn the regex syntax, you can use it for almost any language.
Press ⌃ R to open the search and replace pane and click to enable regular expressions.
Enter a search string in the top field and a replace string in the bottom field. For example, for finding the snake-case written words (
snake_case
), type_(.)
. For replacing them with camel case (camelCase
), type\U$1
. All found occurrences will be highlighted, and the corresponding replacement hints will be available:Click Replace or press ⏎ to replace the current occurrence, Replace All to replace all occurrences, and or Exclude to skip the current occurrence.
Enable/disable case-sensitive search in regular expressions
To enable case-sensitive search, click next to the search string. The search results will match the case of the range specified in the regular expression, for example:
To disable case-sensitive search, click next to the search string. The search results will include both lower- and upper-case letters:
Escape regex special characters in the search filed
Press ⌃ F to open the search field and enable regular expression by clicking .
In the editor, select a code fragment that you want to use as a regular expression and press ⌃ F. The code will be copied to the search string, and the regex special characters (such as
.[{()\^$|?*+
) will be escaped with\
. You can use the generated string for your regular expression, for example:
Use regex capturing groups and backreferences
You can put the regular expressions inside brackets in order to group them. Each group has a number starting with 1, so you can refer to (backreference) them in your replace pattern. Note that the group 0 refers to the entire regular expression. However, you can refer to the captured group not only by a number $n
, but also by a name ${name}
.
For example, for the numbered capturing groups, use the following syntax:
For the named capturing groups, use the following syntax:
Find and replace a captured group
Let's consider the following HTML code:
Open the search and replace pane ⌃ R.
In the search field, enter parentheses
()
that would indicate a capturing group, for example:\svalue="(.*)?"\s*(>*)
.In the replace field, backreference such groups by numbers starting with 1, for example:
placeholder="$1"
AppCode highlights the found occurrences based on your search specifications and displays hints with the replace string.
Switch the character case
You can use regular expressions to change the case of characters that matches some criteria.
Open the search and replace pane ⌃ R. Make sure that is selected in the search field.
In the search field enter the search pattern.
In the replace field, depending on what you want to achieve, enter one of the following syntax:
\l
changes a character to lowercase until the next character in the string.For example,
Bar
becomesbar
.\u
changes a character to uppercase until the next character in the string.For example,
bar
becomesBar
.\L
changes characters to lowercase until the end of the literal string\E
.For example,
BAR
becomesbar
.\U
changes characters to uppercase until the end of the literal string\E
.For example,
bar
becomesBAR
.
Refer to the RegEx syntax reference table for more details.