Regular expressions replace

<< Click to Display Table of Contents >>

Navigation:  Reference Manual > Definitions >

Regular expressions replace

Regular expressions are a form of syntax used to describe pieces of text to be searched for and replaced.

There are many online resources including http://www.regular-expressions.info that describe how to write a regular expression for .NET programs.

See Regular expressions find for more details about search strings

The replace text (called a substitution) will replace every instance of the found text.

The only special character in substitution texts is $.  To replace with a $ use:

\$

The replace text will replace only the text found by the find text.  Great care is needed with find texts to make sure that all the text to be replaced has been captured.

You can include captured text in the replacement by using groups and using:

$1 for the first group

$2 for the second group

and so on

To include characters in the search that are not going to be replaced use a non-capture group:

(?<=...) before the text to be replaced

(?=...) after the text to be replaced

Example regular expression substitutions for name searches

Find text...

Replace

with...

Description

Examples

of changes

A3

A4

This will find all texts containing A3 and replace it with A4

QA3X to QA4X

^Q5

Q6

This will find all texts beginning with Q5 and replace it with Q6

Q51 to Q61

^Q5.*A$

Q6

This will find all texts beginning with Q5 and ending with A and replace the whole text with Q6 because the whole text was included in the search

Q5AA to Q6

^(Q[0-9])

B$1

This will find all names beginning with Q followed by a digit and add a B to the front of the name

Q3C to BQ3C

^V([0-9]+)([A-Z]+)

V$2$1

This will find all names beginning with V followed by one or more digits followed by one or more letters and will swap the numbers and letters

V23A to VA23.

^(Q.*[0-9])$

$1B

This will find all names beginning with Q and ending with a digit and add a B at the end of the name

Q1C43 to Q1C43B

^Q5(?=.*A$)

Q6

This will find all names beginning with Q5 and ending with A and replace just the Q5 with Q6.

Q512A to Q612A

(?<=^Q5.*)A$

B

This will find all names beginning with Q5 and ending with A and replace just the A with B

Q5FA to Q5FB