PDA

View Full Version : [SOLVED:] Replace text found after text



newbie101
10-06-2013, 11:23 PM
Hi Folks,

I have a long document that has multiple occurrences of a string beginning with the word [Start] and ending with [End], the contents of the string itself differ. I need to find all the strings that begin with the word [Start] and end with [End], and add This is the between the opening bracket and End. the end result will be [Start] yada yada etc. [This is the End].

The document contains many [] that are not part of a string that begins with [Start], I don't want to add the text to them.

I've been struggling with different ways and either This is the is added to all [], or it only adds it to the first occurrence.

macropod
10-07-2013, 03:45 AM
So why not simply search for [End] and replace that with [This is the End]?

MacroShadow
10-07-2013, 03:54 AM
.

newbie101
10-07-2013, 03:58 AM
There are many occurrences of [End] that are not part of a string that begins with [Start], I don't want to add the text to them.

Sorry that's what I meant.

EricFletcher
10-07-2013, 06:45 AM
You could use a wildcard Find and Replace -- but with a caveat.

Find what: (\[Start\])(?{1,})(\[End\])
Replace with: \1\2[This is the end]
Use wildcards

The pattern will find [Start] and one or more characters and [End]. I've added color to make it easier to follow; the \ character is needed as an escape because the [ and ] characters have special meaning within wildcard. The parentheses sets defines 3 different required parts of the pattern, and the replace will use the 1st and 2nd parts, but then use the [This is the end] for the 3rd part.

The caveat is that it will act on all instances of [Start] through to [End], so if you have any strings not properly delimited, they could cause problems. For example, [Start]this sentence has a [Falsestart] within it but which of the [End] delimiters defines the string you want?[End] My pattern above will result in the following (bold added):

[Start]this sentence has a [Falsestart] within it but which of the [This is the end] delimiters defines the string you want?[End]

macropod
10-07-2013, 02:25 PM
You could do this with a wildcard Find/Replace, where:
Find = (\[Start\]*\])(End\])
Replace = \1This is the \2
Note: This assumes the paired Start/End sequences have no unpaired '[End]' blocks within them; otherwise there is no way for Find/Replace or a macro to determine from what you have posted which sequences constitute the matched pairs.