The specifics depend on the language you are using, but here are a few general approaches for doing this using regular expression (Python code examples):
Find all matches of your target string, and then combine each match in one string:
>>> import re >>> s = 'the quick brown quick fox' >>> ''.join(re.findall('quick', s)) 'quickquick'
Create a regex to match everything except your target string, and then replace each match with an empty string (this is usually much more complicated than other alternatives):
>>> re.sub('(?!quick|(?<=q)uick|(?<=qu)ick|(?<=qui)ck|(?<=quic)k).', '', s) 'quickquick'
Use capture groups to match everything until the target line appears, and then replace it only with the target line:
>>> re.sub('.*?(quick|$)', r'\1', s) 'quickquick'
If your line has several lines, as in your example, you can first break lines into line breaks or adapt solutions to maintain line breaks, for example:
>>> s = '''the quick brown fox ... the brown fox ... the quick brown quick fox''' >>> print ''.join(re.findall('quick|[\r\n]', s)) quick quickquick >>> print re.sub('.*?(quick|$)', r'\1', s, flags=re.MULTILINE) quick quickquick
source share