JuanjoC
JuanjoC

Reputation: 195

Modify position in a line if Regular Expression found

I need to modify the positions number 10 of every line that finds the word 'Example' (can´t use the actual data here) and add the string '(ID) '. It doesn´t necessarily have to begin with 9 numbers, it just needs to add the string to the position number 10. For example, this line should be modified like this:

So far I have this, to find the Example and copy the rest of the line as to not lose the text:

Find: (.*)Example

Bonus points if it works for two different words 'Example1' and 'Example2' in different sentences, the 'and also' part of this example would change in every line.

This would have this search:

Find: (.*)Example1(.*)Example2

Thank you

Upvotes: 0

Views: 30

Answers (2)

JuanjoC
JuanjoC

Reputation: 195

This is how you add characters in a certain position, even tho I accepted Tims answer because it´s very similar and made me figure it out:

^(\S{9})(?=.*\bExample1\b.*\bExample2\b)

As you can see, I only added '^' so it´s the position from the start of the line, and 'S' instead of 'd' so it counts characters that are not whitespace, instead of numbers. This should work for any type of line you have.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521399

You could try:

Find:    (\d{9})(?=.*\bExample1\b.*\bExample2\b)
Replace: $(ID) 
               ^^^ single space after (ID)

Demo

The regex pattern used matches and captures a 9 digit number (you may adjust to any width, or range of widths, which you want). It also uses a positive lookahead to assert that Example1 and Example2 in fact occur later in the same line:

(?=.*\bExample1\b.*\bExample2\b)

Upvotes: 1

Related Questions