Regex - find and replace complete string occurrences only (not partial matches)

Posted by vittore on Stack Overflow See other posts from Stack Overflow or by vittore
Published on 2010-04-02T23:58:34Z Indexed on 2010/04/03 0:53 UTC
Read the original article Hit count: 549

Filed under:
|
|
|

I'm not very good at regex but maybe there's a simple way to achieve this task.

I'm given a string like "bla @a bla @a1 bla"

I'm also given pairs like {"a", "a2"} , {"a1", "a13"}, and I need to replace @a with @a2 for the first pair, and @a1 with @a13 for the second one.

The problem is when i use String.Replace and look for @a, it also replaces @a1 but it should not. I need it to completely match @a and avoid partially matching it in other places.

Note: the given string could also be brackets, commas, dots and so on. However, pairs will always be [a-z]*[0-9]+

Help me with regex replace, please.

Cheers

© Stack Overflow or respective owner

Related posts about regex

Related posts about replace