Question

Let's say I have the following text:

Lorem ipsum dolor sit amet, consectetur aaBaaBaaB adipiscing elit.

aaBaaB

aaB Ut in risus quis elit posuere faucibus sed vitae metus. aaBaaBaaBaaB

Fusce nec tortor in dolor aaBaaBaaB porttitor viverra. aaB

I'm trying to figure out how to perform a regular expression search and replace on this in such a way that the output is:

Lorem ipsum dolor sit amet, consectetur aaBaaB adipiscing elit.

aaB

Ut in risus quis elit posuere faucibus sed vitae metus. aaBaaBaaB

Fusce nec tortor in dolor aaBaaB porttitor viverra.

That is, to remove one "aaB" from each pattern of it. Is this actually possible, and if so, how would it be done? Specifically, I intend to do this in Sublime Text 2 as a RegEx search/replace in a file.

Was it helpful?

Solution

You can use a positive lookahead:

(?=(?<w>[a-z]{2}[A-Z]{1})\s)\k<w>

You just need to make sure you have case-sensitive matching on.

example: http://regex101.com/r/sK8bG1

OTHER TIPS

Use either the leading or trailing whitespace to remove the first or last substring. Either of these work:

(\s+)(aaB) with $1 in the Replace field

or

(aaB)(\s+) with $2 in the Replace field

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top