Loading...
Replaced: 0
Case insensitive.   Allow regex matching in find field.   Display only if options.  

Tool: Find and replace text.

Purpose: Find and replace text across line breaks with the option of regular expression (regex) matching and controlling replacement text based upon matching text containing or not containing a specific text or regex.
Basic instructions: Load working-text, enter find text into "Find this text or regex" field, enter replacement text into "and replace with" field and click "Find and replace" button.
Advance instructions: Check "Allow regex matching in find field" checkbox to enable "regular expression" matching in the find field. When using regex find matching, the "and replace with" replacement action can be controlled by matching another text or regex by checking the "Display only if options" checkbox. only if matching controls whether the "Find this text or regex" match will be replaced depending upon the found text containing or not containing the only if field's text or regex. See example below.
Regex instructions: A "regular expression" (regex) is a sequence of characters defines a search pattern within a matching action. Within a regex phrase are literal and special characters of which the special characters define what actions are to be done. Special characters []{}().*+?^=!:$\| will function as literal characters when escaped with a single backslash.

The simplest and most useful regex character is the pipebar | which is interpreted as "or" within the phrase. To find literal characters ("a" or "b" or "c") within working-text, use the regex phrase a|b|c. See example below. Remember to escape special characters when used as literal characters within the phrase, thus ("1+1" or "1=1" or "1*1") will read as 1\+1|1\*1|1\=1. Use the "Enter literal find text to escape special characters" tool at page bottom to escape a mix of literal and special characters used as literals.

A regex capture can be created by placing parentheses around part of a regex grouping to create a backreference to the matched text and applied as a placeholder ($1) within the replace text. A common use for capture/placeholder would be to apply text around matched text. For example to apply html bold tags around ("a" or "b" or "c") use the regex a|b|c but within parentheses looking as (a|b|c). The replacement text will now use the matched text as the backreference placeholder written as $1 replacing itself with itself and whatever is included around it. The replacement text will read as <b>$1</b> which will wrap bold tags around all instances of ("a" or "b" or "c") found within text. Only three captures/placeholders ($1, $2, $3) will function in this tool, so $4, $5, $6 need not be applied. See examples: Wrap text around captured placeholder text. and Swap two regex captures using $1 and $2 placeholders.
Regex examples: Below are regex examples that may meet your needs or be altered to meet your needs. Be sure to check "Allow regex matching in find field" checkbox to enable regex find matching.
Replace ("a" or "b" or "c") with "d".

Working-text is:

A box of biscuits, a box of mixed biscuits and a biscuit mixer!

Enter regex a|b|c into "Find this text or regex" field, d into "and replace with" field and click "Find and replace" button to produce:

d dox of disduits, d dox of mixed disduits dnd d disduit mixer!



Wrap text around captured placeholder text.

Text captured within the regex parentheses can be placed back (backreference) into text as a placeholder with text wrapping around it. This is useful for wrapping tags around text, escaping special text, etc. In this example the HTML bold tag <b>text</b> will be wrapped around captured text using the placeholder "$1".

Working-text is:

Place bold tags around the words bold and tag.

Enter regex (\bbold\b|\btag\b)* into "Find this text or regex" field, enter <b>$1</b> into "and replace with" field and click "Find and replace" button to produce:

Place <b>bold</b> tags around the words <b>bold</b> and <b>tag</b>.

* \b matches a word boundary so capture will only match "tag" and not "tags".

Note: Only three captures/placeholders ($1, $2, $3) will function with this tool.



Swap two regex captures using $1 and $2 placeholders.

Working-text is:

abcd

To swap letters "b" and "c" enter the regex grouping (b)(c) into find field, enter $2$1 into replace field and click "Find and replace" button to produce:

acbd



Replace differing text between two strings.

Working-text is:

1. start abc1 end
2. start ab2 end
3. start abc3 end


Goal is to replace differing text (in differing situations) between the strings/words "start" and "end".

To replace all instances (abc1,ab2,abc3) you will need to use the regex grouping .*? between strings within lines producing the regex phrase \bstart\b.*?\bend\b* to be entered in the find field. In the replace field enter start (replacement text) end and click "Find and replace" button to produce:

1. start (replacement text) end
2. start (replacement text) end
3. start (replacement text) end


*Use \b to denote a word boundary so match will only occur with start/end and not within other words like starter or send.



Replace differing text between two strings conditional to match containing a specific text.

Working-text is:

1. start abc1 end
2. start ab2 end
3. start abc3 end


Enter regex phrase \bstart\b.*?\bend\b* in the find field and start (replacement text) end in the replace field. To only replace instances conditional to containing or not containing a specific text check "Display only if options" checkbox. Within the "this text or regex" field enter conditional only if text or regex. For demonstration enter 2 into only if field, check "containing" radio button and click "Find and replace" button to produce:

1. start abc1 end
2. start (replacement text) end
3. start abc3 end


Checking the "not containing" button will produce inverse results. To prevent the conditional only if matching from matching text outside of it's grouping \bstart\b(.*?)\bend\b check the "Use find field's captured text $1 for only if matching" checkbox. A placeholder is the matching text captured by the regex phrase within the parentheses grouping represented by "$1". In this example that would be text between "start " and " end" with no matching to either "start" or "end".



Replace differing text between two strings conditional to captured matching text containing both "b" and "c".

Working-text is:

1. start abc1 end
2. start ab2 end
3. start abc3 end


Enter regex phrase \bstart\b.*?\bend\b in the find field and start (replacement text) end in the replace field. For conditional only if matching of captured text that must contain both "b" and "c", enter (?=.*b)(?=.*c) into the only if field, check both "Use find field's captured text for only if matching" and "Allow regex matching in only if field" checkbox. Click "Find and replace" button to produce:

1. start (replacement text) end
2. start ab2 end
3. start (replacement text) end




Replace text if followed by another text.

Working-text is:

1. start abc1 end
2. start ab2 end
3. start abc3 end


To match a text followed by another text is a called "lookahead". A lookahead phrase is written as a(?=b) for matching "a" if followed by "b" or a(?=b|c) for matching "a" if followed by "b" or "c". Enter regex phrase a(?=2) in the find field and start (replacement text) end in the replace field, click "Find and replace" button to produce:

1. start abc1 end
2. start (replacement text) end
3. start abc3 end




Replace text if not followed by another text.

Working-text is:

1. start abc1 end
2. start ab2 end
3. start abc3 end


To match a text followed by another text is a called "negated lookahead". A negated lookahead phrase is written as a(?!b) for matching "a" if not followed by "b" or a(?!b|c) for matching "a" if not followed by "b" or "c". Enter regex phrase b(?!2) in the find field and start (replacement text) end in the replace field, click "Find and replace" button to produce:

1. start (replacement text) end
2. start ab2 end
3. start (replacement text) end




Remove lines containing a specific text.

Working-text is:

1. start abc1 end
2. start ab2 end
3. start abc3 end


Splitting text by line breaks for matching text within it's lines can be accomplished with regex [^\n]+. To remove lines containing "bc" enter [^\n]+ in the find field, empty replace field, check "Allow regex matching in find field" checkbox, check "Display only if options" checkbox, check "contains" radio button, enter bc into "this text or regex" field and click "Find and replace" button to produce:


2. start ab2 end



Removed line content will leave a blank lines which can be removed using the "Remove blank lines" tool.



Skip pipe bar.