Tastybrownies
Tastybrownies

Reputation: 937

Using Regex in Notepad++ to Replace a Character

I have a large notepad++ file and have an instance where a field has 16 length that can contain numbers and letters followed by a comma and then another field that starts with letters. I am trying to make sure the next field after the comma starts with a ".

So far I have this regex. I checked it with ruby expressions and it appears right. I just don't know how to insert a " after the comma to begin the next field. This is probably really easy I am just missing something here. Any help on this would be greatly appreciated!

([a-zA-Z0-9]{16}\,[a-zA-Z]+)

So I want to add a " after the escaped comma above.

Upvotes: 0

Views: 2536

Answers (2)

Ken
Ken

Reputation: 654

This should do it:

([a-zA-Z0-9]{16}\,\"[a-zA-Z]+)

Upvotes: 1

Martin Ender
Martin Ender

Reputation: 44289

There are two options.

You could use capturing and write back what you matched (in addition to the quotes):

([a-zA-Z0-9]{16},)([a-zA-Z]+)

Each set of parentheses generates a captured group. You can reference what they matched with $n in the replacement string: $1"$2.

Note that there is no need to escape the comma.

The alternative is lookarounds. What you match inside a lookaround is not part of the match, so it doesn't get removed. If you put everything around the desired position in lookarounds, you'll match a position instead of a string, and the replacement string will simply be inserted in that position:

(?<=[a-zA-Z0-9]{16},)(?=[a-zA-Z]+)

And replace that with ".

In any case, make sure to upgrade to Notepad++ 6.

Finally note that in both cases the + at the end is irrelevant - if there's one letter, there is also "one or more letters". (There is a very unlikely edge-case where it would make a difference, but that is probably beyond the scope of this question.)

Upvotes: 1

Related Questions