Re: Replace character

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hello shapper,

On Nov 20, 2:33 am, Arne Vajhøj <a...@xxxxxxxxxx> wrote:

shapper wrote:

I have a text as follows:

"My email is someth...@xxxxxxxxxxxxx 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(text, "\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+)+)@(?<domain>\w+([-.]\w+)*\.\w+([-.]\w+)*))

and replace that with:

${name}(at)${domain}

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


.



Relevant Pages

  • Re: Inline String Replace, there has to be a better way
    ... Regex has a Replace method with a MatchEvaluator callback you can use to select ... I'm hoping the Regex engine is doing the *right* thing behind the scenes and I'm ... (note I found a way to do a basic replacement rather than a MatchEvaluator ... > The code below does not seem very efficient if you have a very long string> and are doing many replace functions, there has to be a better way to do> this without reassigning the value type string over and over, any> suggestions? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Return Data Regex Doesnt Isolate - Yikes
    ... string as the replacement if you didn't need to access any of the match ... > I'm having a bad regex day and can sure use your help, ... > Here's some test data ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Replace character
    ... Regex.Replace with a reasonable regex expression for valid email ... addresses should give a reasonable correct replacement. ... The second problem is that this identifies all emails on my string ... Second option is to use verbatim strings ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: gsub and regex
    ... allow a regex as the replacement when a match occurs. ... considerably confused about data types and regex, and just plain need ... Do you want to replace all newline charactes ...
    (comp.lang.ruby)
  • Re: Additional Functions for base classes.
    ... Unless you want to replace them all at the same time, using Regex is a very expensive operation to do single string replacements. ... You can use a regex to specify all of the characters that need to have somthign prefixed by grouping them into a characterset. ... The replacement patterns are a little different than you're used to, because it is possible to include the text you originally found. ...
    (microsoft.public.dotnet.languages.csharp)