Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Replace @. Could someone, please, help me out?

Last post 11-20-2008, 6:06 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  11-20-2008, 11:53 AM 48564

    Replace @. Could someone, please, help me out?

    Hello,

    I am trying to replace the @ by (at) in all emails in a text. Nothing is replaced.

    I tried it in two ways but both don't produce any change:

          string a = "my email is john@gmail.com and @01 or ab@cd are not emails. The following <a href="mary@gmail.com">mary@gmail.com</a> is an anchor";

          string b = Regex.Replace(a, @"(?<name>\w+([-+.]\w+)+)@(?<domain>\w+([-.]\w+)*\.\w+([-.]\w+)*)", "${name}(at)${domain}");

          string c = Regex.Replace(a, @"(?<=\w+([-+.]\w+)+)(at)(?>\w+([-.]\w+)*\.\w+([-.]\w+)*)", "(at)");

    Could someone, please, help me out?

    Thanks,
    Miguel

     

  •  11-20-2008, 6:06 PM 48589 in reply to 48564

    Re: Replace @. Could someone, please, help me out?

    If you change the 'string b' regex to be:

    (?<name>\w+([-+.]\w+)*)@(?<domain>\w+([-.]\w+)*\.\w+([-.]\w+)*)

    (i.e. change the '+' to '*' after the '([-+.]\w+)') then it finds 3 "emails" - one for john@gmail.com and 2 for mary@gmail.com. The replaced text becomes:

    my email is john(at)gmail.com and @01 or ab@cd are not emails. The following <a href="mary(at)gmail.com">mary(at)gmail.com</a> is an anchor

    'string c' won't work because there are no "(at)" strings in the sample text.

    Also, I'm surprised that the computer didn't throw a syntax error in your definition of 'string a' in that there are unescaped double-quotes - perhaps this is a transcription error into the forum.

    Susan

View as RSS news feed in XML