How about this?
gsub("^( abc@abc )|(?<=[ (])( abc@abc )", " anonym@anonym ", tmp, perl=T)
Sample before | : ^( abc@abc ) , of course, checks the beginning with abc@abc .
In the pattern after | uses a positive lookbehind and searches for abc@abc , which is preceded by space or ( (left), and if found, is replaced by anonym@anonym .
This is what I get: (Note: I replaced abc.abc in the last line abc@abc )
[1] " aaaaabc@abc.debbbb " "aaaa anonym@anonym.de bbbb" [3] " anonym@anonym.de " " aaa.abc@abc.de " [5] " aaaa_abc@abc.de " "( anonym@anonym.de )" [7] "aaaa ( anonym@anonym.de ) bbbb"
Edit: To explain the problem with your regex, this seems like a problem with the part:
[^([:alnum:]|.|_)]
I think negation should be present in every expression | . In addition, you should use [.] Instead . , since the latter implies any character. Alternatively, instead of using negation for each character that you test, we can condense this part by removing all unnecessary | as:
[^.[:alpha:]_]
You replace each abc@abc . But you will lose the character before abc@abc every time, because you also check it in the template. So you have to use a capture group. That is, if you complete the regular expression with () , you can refer to this โcaptureโ using special variables such as \\1, \\2 etc.. Here we wrote (^|[^.[:alpha:]_]) , i.e. Part to abc@abc . Since this is the first capture, we will refer to it as \\1 to use it to recover the missing character in the previous result:
gsub("(^|[^.[:alpha:]_]) abc@abc ", "\\ 1anonym@anonym ", tmp) # [1] " aaaaabc@abc.debbbb " "aaaa anonym@anonym.de bbbb" # [3] " anonym@anonym.de " " aaa.abc@abc.de " # [5] " aaaa_abc@abc.de " "( anonym@anonym.de )" # [7] "aaaa ( anonym@anonym.de ) bbbb"
This is the result you need. And this is the same as my original answer using a positive look. In this case, since it just checks that it is preceded by something, you do not need to capture anything. Only the abc@abc been replaced. Hope this helps.