Replace character

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Replace character

    Hello,

    I have a text as follows:

    "My email is something@somet hing.xyz and I posted this @ 2 am"

    I need to replace the @ by (AT) bu only the ones that are in email
    addresses.

    All other @ shouldn't be replaced.

    I know how to replace all @ but I am having problems in replacing only
    the @'s in the email addresses.

    How can I do this?

    Thanks,
    Miguel
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Replace character

    shapper wrote:
    I have a text as follows:
    >
    "My email is something@somet hing.xyz and I posted this @ 2 am"
    >
    I need to replace the @ by (AT) bu only the ones that are in email
    addresses.
    >
    All other @ shouldn't be replaced.
    >
    I know how to replace all @ but I am having problems in replacing only
    the @'s in the email addresses.
    >
    How can I do this?
    Regex.Replace with a reasonable regex expression for
    valid email addresses should give a reasonable correct
    replacement.

    Arne

    Comment

    • shapper

      #3
      Re: Replace character

      On Nov 20, 2:33 am, Arne Vajhøj <a...@vajhoej.d kwrote:
      shapper wrote:
      I have a text as follows:
      >
      "My email is someth...@somet hing.xyz and I posted this @ 2 am"
      >
      I need to replace the @ by (AT) bu only the ones that are in email
      addresses.
      >
      All other @ shouldn't be replaced.
      >
      I know how to replace all @ but I am having problems in replacing only
      the @'s in the email addresses.
      >
      How can I do this?
      >
      Regex.Replace with a reasonable regex expression for
      valid email addresses should give a reasonable correct
      replacement.
      >
      Arne
      Hi,

      I was trying that but I have two problems:
      return Regex.Replace(t ext, "\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)
      *", "(at)");

      First I get a lot of errors on the pattern in each \ character:
      Unrecognized escape sequence

      The second problem is that this identifies all emails on my string but
      I don't want to replace the emails but the @ in the emails.

      Any idea?

      Thanks,
      Miguel

      Comment

      • Jesse Houwing

        #4
        Re: Replace character

        Hello shapper,
        On Nov 20, 2:33 am, Arne Vajhøj <a...@vajhoej.d kwrote:
        >
        >shapper wrote:
        >>
        >>I have a text as follows:
        >>>
        >>"My email is someth...@somet hing.xyz and I posted this @ 2 am"
        >>>
        >>I need to replace the @ by (AT) bu only the ones that are in email
        >>addresses.
        >>>
        >>All other @ shouldn't be replaced.
        >>>
        >>I know how to replace all @ but I am having problems in replacing
        >>only the @'s in the email addresses.
        >>>
        >>How can I do this?
        >>>
        >Regex.Replac e with a reasonable regex expression for valid email
        >addresses should give a reasonable correct replacement.
        >>
        >Arne
        >>
        Hi,
        >
        I was trying that but I have two problems:
        return Regex.Replace(t ext, "\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)
        *", "(at)");
        First I get a lot of errors on the pattern in each \ character:
        Unrecognized escape sequence
        >
        The second problem is that this identifies all emails on my string but
        I don't want to replace the emails but the @ in the emails.

        There are two options here: Either capture the first and second part of the
        email in a group and put them back in.

        Second, match only the @, but use lookahead and lookbehind constructions
        to make sure you only replace the @ in an emailaddress

        So here we go:

        capture the first and last parts and replace:

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

        and replace that with:

        ${name}(at)${do main}

        This should be quite easy to understand. Capture the parts you want to keep
        in a named group (?<name>..) and put them back into the replacement pattern
        ${name}.



        The second option using look arounds:

        (?<=\w+([-+.]\w+)+)(at)(?>\w +([-.]\w+)*\.\w+([-.]\w+)*))

        and replace that with:

        (at)

        This one is usually harder to understand. (?<=...) looks back in your string
        and tries to find the pattern that is defined at the '...'. If it cannot
        find this exact pattern the whole expression fails.
        (?>...) essentially does the same, but looks ahead, instead of backwards.
        The funny thing is, that even though you search for something in your regex,
        it doesn't become part of the actual Match and therefore doesn't get replaced.



        Now to touch on your issue with th escape sequences. Any special character
        sequence in regex starts with a \. And any escape sequence in C# code starts
        with a \ as well, so if you put a regex in a string you again have two options:
        First, escape all your \'s in your regex by putting an additional \ in front
        of it. eg. \w becomes \\w. Second option is to use verbatim strings in C#
        by placing an @ in front of the string definition like this: @"\w...more
        regex goes here".

        Hope this helps

        --
        Jesse Houwing
        jesse.houwing at sogeti.nl


        Comment

        • shapper

          #5
          Re: Replace character

          On Nov 20, 11:12 am, Jesse Houwing <jesse.houw...@ newsgroup.nospa m>
          wrote:
          Hello shapper,
          >
          >
          >
          On Nov 20, 2:33 am, Arne Vajhøj <a...@vajhoej.d kwrote:
          >
          shapper wrote:
          >
          >I have a text as follows:
          >
          >"My email is someth...@somet hing.xyz and I posted this @ 2 am"
          >
          >I need to replace the @ by (AT) bu only the ones that are in email
          >addresses.
          >
          >All other @ shouldn't be replaced.
          >
          >I know how to replace all @ but I am having problems in replacing
          >only the @'s in the email addresses.
          >
          >How can I do this?
          >
          Regex.Replace with a reasonable regex expression for valid email
          addresses should give a reasonable correct replacement.
          >
          Arne
          >
          Hi,
          >
          I was trying that but I have two problems:
          return Regex.Replace(t ext, "\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)
          *", "(at)");
          First I get a lot of errors on the pattern in each \ character:
          Unrecognized escape sequence
          >
          The second problem is that this identifies all emails on my string but
          I don't want to replace the emails but the @ in the emails.
          >
          There are two options here: Either capture the first and second part of the
          email in a group and put them back in.
          >
          Second, match only the @, but use lookahead and lookbehind constructions
          to make sure you only replace the @ in an emailaddress
          >
          So here we go:
          >
          capture the first and last parts and replace:
          >
          (?<name>\w+([-+.]\w+)+)@(?<domai n>\w+([-.]\w+)*\.\w+([-.]\w+)*))
          >
          and replace that with:
          >
          ${name}(at)${do main}
          >
          This should be quite easy to understand. Capture the parts you want to keep
          in a named group (?<name>..) and put them back into the replacement pattern
          ${name}.
          >
          The second option using look arounds:
          >
          (?<=\w+([-+.]\w+)+)(at)(?>\w +([-.]\w+)*\.\w+([-.]\w+)*))
          >
          and replace that with:
          >
          (at)
          >
          This one is usually harder to understand. (?<=...) looks back in your string
          and tries to find the pattern that is defined at the '...'. If it cannot
          find this exact pattern the whole expression fails.
          (?>...) essentially does the same, but looks ahead, instead of backwards.
          The funny thing is, that even though you search for something in your regex,
          it doesn't become part of the actual Match and therefore doesn't get replaced.
          >
          Now to touch on your issue with th escape sequences. Any special character
          sequence in regex starts with a \. And any escape sequence in C# code starts
          with a \ as well, so if you put a regex in a string you again have two options:
          First, escape all your \'s in your regex by putting an additional \ in front
          of it. eg. \w becomes \\w. Second option is to use verbatim strings in C#
          by placing an @ in front of the string definition like this: @"\w...more
          regex goes here".
          >
          Hope this helps
          >
          --
          Jesse Houwing
          jesse.houwing at sogeti.nl
          Hello,

          I tried to use your code but in both expressions I get the error "to
          many )'s".
          I counted the ( and ) and removed the last ) in both expressions.

          Now I don't get the error but the @ in the emails are not replaced.
          This is what I have:

          string a = "my email is name@dm.com and @01 and noemail@test are
          not emails but name2@md.net is again";

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

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

          Am I doing something wrong?

          Thanks,
          Miguel

          Comment

          • Jesse Houwing

            #6
            Re: Replace character

            Hello shapper,
            On Nov 20, 11:12 am, Jesse Houwing <jesse.houw...@ newsgroup.nospa m>
            wrote:
            >
            >Hello shapper,
            >>
            >>On Nov 20, 2:33 am, Arne Vajhøj <a...@vajhoej.d kwrote:
            >>>
            >>>shapper wrote:
            >>>>
            >>>>I have a text as follows:
            >>>>>
            >>>>"My email is someth...@somet hing.xyz and I posted this @ 2 am"
            >>>>>
            >>>>I need to replace the @ by (AT) bu only the ones that are in email
            >>>>addresses .
            >>>>>
            >>>>All other @ shouldn't be replaced.
            >>>>>
            >>>>I know how to replace all @ but I am having problems in replacing
            >>>>only the @'s in the email addresses.
            >>>>>
            >>>>How can I do this?
            >>>>>
            >>>Regex.Replac e with a reasonable regex expression for valid email
            >>>addresses should give a reasonable correct replacement.
            >>>>
            >>>Arne
            >>>>
            >>Hi,
            >>>
            >>I was trying that but I have two problems:
            >>return Regex.Replace(t ext,
            >>"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)
            >>*", "(at)");
            >>First I get a lot of errors on the pattern in each \ character:
            >>Unrecognize d escape sequence
            >>The second problem is that this identifies all emails on my string
            >>but I don't want to replace the emails but the @ in the emails.
            >>>
            >There are two options here: Either capture the first and second part
            >of the email in a group and put them back in.
            >>
            >Second, match only the @, but use lookahead and lookbehind
            >construction s to make sure you only replace the @ in an emailaddress
            >>
            >So here we go:
            >>
            >capture the first and last parts and replace:
            >>
            >(?<name>\w+([-+.]\w+)+)@(?<domai n>\w+([-.]\w+)*\.\w+([-.]\w+)*))
            >>
            >and replace that with:
            >>
            >${name}(at)${d omain}
            >>
            >This should be quite easy to understand. Capture the parts you want
            >to keep in a named group (?<name>..) and put them back into the
            >replacement pattern ${name}.
            >>
            >The second option using look arounds:
            >>
            >(?<=\w+([-+.]\w+)+)(at)(?>\w +([-.]\w+)*\.\w+([-.]\w+)*))
            >>
            >and replace that with:
            >>
            >(at)
            >>
            >This one is usually harder to understand. (?<=...) looks back in your
            >string
            >and tries to find the pattern that is defined at the '...'. If it
            >cannot
            >find this exact pattern the whole expression fails.
            >(?>...) essentially does the same, but looks ahead, instead of
            >backwards.
            >The funny thing is, that even though you search for something in your
            >regex,
            >it doesn't become part of the actual Match and therefore doesn't get
            >replaced.
            >Now to touch on your issue with th escape sequences. Any special
            >character
            >sequence in regex starts with a \. And any escape sequence in C# code
            >starts
            >with a \ as well, so if you put a regex in a string you again have
            >two options:
            >First, escape all your \'s in your regex by putting an additional \
            >in front
            >of it. eg. \w becomes \\w. Second option is to use verbatim strings
            >in C#
            >by placing an @ in front of the string definition like this:
            >@"\w...more
            >regex goes here".
            >Hope this helps
            >>
            >--
            >Jesse Houwing
            >jesse.houwin g at sogeti.nl
            Hello,
            >
            I tried to use your code but in both expressions I get the error "to
            many )'s".
            I counted the ( and ) and removed the last ) in both expressions.
            Now I don't get the error but the @ in the emails are not replaced.
            This is what I have:
            >
            string a = "my email is name@dm.com and @01 and noemail@test are
            not emails but name2@md.net is again";
            >
            string b = Regex.Replace(a , @"(?<name>\w +([-+.]\w+)+)@(?<domai n>
            \w+([-.]\w+)*\.\w+([-.]\w+)*)", "${name}(at)${d omain}");
            >
            string c = Regex.Replace(a , @"(?<=\w+([-+.]\w+)+)(at)(?>\w +([-.]
            \w+)*\.\w+([-.]\w+)*)", "(at)");
            >
            Am I doing something wrong?

            Loosk liek I made an error while copying part of your expression:
            string b = Regex.Replace(a , @"(?<name>\w +([-+.]\w+)*)@(?<domai n>
            \w+([-.]\w+)*\.\w+([-.]\w+)*)", "${name}(at)${d omain}");
            >
            string c = Regex.Replace(a , @"(?<=\w+([-+.]\w+)*)(at)(?>\w +([-.]
            \w+)*\.\w+([-.]\w+)*)", "(at)");
            Try these...
            --
            Jesse Houwing
            jesse.houwing at sogeti.nl


            Comment

            Working...