Find and Replace Strings in Code

To search for a string in code, place the cursor in the code editor, then press Ctrl+F to open the Quick Search box in search mode, or Ctrl+H to open it in replace mode. Alternatively, use the appropriate command from the context menu. To toggle between the two modes, click the up or down icon, or press Ctrl+F or Ctrl+H.

Quick Search box in Replace mode

The search is restricted to the code of the current object.

  • To find a string, Closedtype it in the search field and click the Search button or press Enter.

    The next occurrence of the string (if found) is displayed and selected in the Code Editor. If the string is not found, this is reported in the Messages tab.
  • To find and replace a string, Closedtype the search string in the search field and the replace string in the replace field. Then click the Replace button.

    The next occurrence of the search string (if found) is displayed and selected in the Code Editor.

    Click the Replace button again to replace the selected text with the replacement string, and then search for the next occurrence of the search string. If the search string is not found, this is reported in the Messages tab.
  • To replace all occurrences of a search string, Closedtype the search string in the search field and the replace string in the replace field. Then click the All button.

    All occurrences of the search string are replaced by the replace string. The Messages tab shows the number of replacements made. If the search string is not found, this is reported in the Messages tab.
  • To set search options, Closedclick the option to switch it on or off.
    Quick Search Commands
    Icon Command Description
    Search backwardsSearch backwardsSearch backwards. The default search direction is forwards.
    Match letter caseMatch caseFind matching strings that also match the case of the search string.
    Match whole wordMatch whole wordFind matching strings that are preceded or followed by non-word characters such as spaces, commas, and periods.
    Match start of wordMatch start of wordFind matching strings that are preceded by non-word characters such as spaces.
    Use regular expressionsUse regular expressionsInterpret the search string as a regular expression.
    Loop though the document

    Wrap around

    When the end of the code container is reached, continue searching at the beginning (or the other way around, when searching backwards).
  • To search using regular expressions, Closedyou can use the following symbols:
    Symbols for Regular Expressions
    Symbol Description
    . Matches any character
    \( Marks the start of a region for tagging a match.
    \)Marks the end of a tagged region.
    \< Matches the start of a word.
    \>Matches the end of a word.
    \XUse a character X that would otherwise have a special meaning. For example, \[ is interpreted as [ and not as the start of a character set.
    [...]Defines the set of allowed characters to match. For example, [abc] means any of the characters a, b or c.

    You can also specify a range of characters. For example, [a-z] matches any lower case character.

    [^...] Defines the set of characters to be excluded from the match. For example, [^A-Za-z] means any character except an alphabetic character.
    ^ Matches the start of a line (unless used inside a set (as defined by [])
    $ Matches the end of a line.
    ? Matches zero or one times the specified character. For example, Sa?m matches Sm or Sam.
    +Matches 1 or more times the specified character. For example, Sa+m matches Sam, Saam, Saaam and so on.
    * Matches 0 or more times the specified character. For example, Sa*m matches Sm, Sam, Saam, Saaam and so on.

 

Examples of regular expressions in searches

  • Find where a variable (sum) is assigned a value:

    ^\s*sum\s*=

  • Find where a variable (sum) is used:

    \<sum\>.

    The use of \< and \>, which match the start and end of a word respectively, ensures that the expression won’t find strings like SUMMARY, CHECKSUM or CONSUMPTION.

  • Remove trailing blanks:

    \s+$

    then replace with nothing.

  • Update string substitution syntax:

    %%([^%]+)%%%

    then replace with:

    %%(\1)

    For more information, see Substitution in String Values.