Navigation:  Actions > Files > Replace in File >

Replace in File Action Text Tab

Previous pageReturn to chapter overviewNext page

This tab of the Replace in File action specifies the text to be found and replaced in the file.


Text to find: The text or regular expression to find.  This can contain literal text or a regular expression specifying one or more items to match (see below).



To insert literal bracket [ ] and percent sign % characters within this field, they must be doubled up (just as in any other field), since these are normally interpreted by Visual Build as referencing script code and macros within a field.
For regular expressions (non-literal) matching, . (dot) matches newline characters by default.  Uncheck the Dot does not match newline option on the Replace tab to prevent . from matching newline characters and perform line-by-line matching.


Replace matches with: The text to replace any matches with.


For literal text, this can be a single search and replace value, with the no regular expressions field checked.  To search and replace multiple literal values, enter each value on a separate line (the number of search and replace lines must match).  For instance, to change all occurrences of 'fox' with 'cat' and 'dog' with 'beagle', the find expression





and replace expression





could be used.  If a single-line string is provided in the find field and the replace text contains multiple lines, the value will be replaced with the entire multi-line value from the replace field.



For regular expressions (the literal option is unchecked on the Replace tab), each item within parentheses is grouped as a numbered expression, which can be matched in the replace field.  Nested parentheses can be used to identify values within an overall matching value; each new parenthesis is numbered sequentially (starting at 1) in the order that the opening parenthesis characters are found in the string.  Replacement groups are identified by ?n, and $n will be replaced with the matching expression in the search expression.


More complex expressions, such as the one in the dialog above can also be defined (see the samples for more Replace in File samples).  For example, these expressions change any DIALOG resources in a resource file to DIALOGEX and FONT "MS Sans Serif" to FONT "MS Shell Dlg":


Find text:

(^IDD([[^\s]]*\s+)DIALOG\s+)|(^FONT([[^"]]*)\"MS Sans Serif\")


Replace text:

(?1IDD$2DIALOGEX )(?3FONT$4"MS Shell Dlg")


Text to append to file if no matches found: If provided and no matches are found for the Text to find field, the text in this field will be appended to the output file.  This can be useful for adding a missing value only if it is not already in the file (the find/replace fields are used to modify an existing value if found).  This field is treated as literal text (after any macros or script expressions have been evaluated) and should not contain regular expression replacement variables.