473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Replace character

Hello,

I have a text as follows:

"My email is so*******@something.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
Nov 20 '08 #1
5 2561
shapper wrote:
I have a text as follows:

"My email is so*******@something.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
Nov 20 '08 #2
On Nov 20, 2:33*am, Arne Vajhøj <a...@vajhoej.dkwrote:
shapper wrote:
I have a text as follows:
"My email is someth...@something.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(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.

Any idea?

Thanks,
Miguel
Nov 20 '08 #3
Hello shapper,
On Nov 20, 2:33 am, Arne Vajhøj <a...@vajhoej.dkwrote:
>shapper wrote:
>>I have a text as follows:

"My email is someth...@something.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(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
Nov 20 '08 #4
On Nov 20, 11:12*am, Jesse Houwing <jesse.houw...@newsgroup.nospam>
wrote:
Hello shapper,
On Nov 20, 2:33 am, Arne Vajhøj <a...@vajhoej.dkwrote:
shapper wrote:
>I have a text as follows:
>"My email is someth...@something.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(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
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 na**@dm.com and @01 and noemail@test are
not emails but na***@md.net is again";

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)");

Am I doing something wrong?

Thanks,
Miguel
Nov 20 '08 #5
Hello shapper,
On Nov 20, 11:12 am, Jesse Houwing <jesse.houw...@newsgroup.nospam>
wrote:
>Hello shapper,
>>On Nov 20, 2:33 am, Arne Vajhøj <a...@vajhoej.dkwrote:

shapper wrote:

I have a text as follows:
>
"My email is someth...@something.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(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
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 na**@dm.com and @01 and noemail@test are
not emails but na***@md.net is again";

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)");

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+)*)@(?<domain>
\w+([-.]\w+)*\.\w+([-.]\w+)*)", "${name}(at)${domain}");

string c = Regex.Replace(a, @"(?<=\w+([-+.]\w+)*)(at)(?>\w+([-.]
\w+)*\.\w+([-.]\w+)*)", "(at)");
Try these...
--
Jesse Houwing
jesse.houwing at sogeti.nl
Nov 21 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Billy N. Patton | last post by:
-------- Original Message -------- Subject: <string>.replace Date: Fri, 15 Oct 2004 11:07:19 -0500 From: Billy N. Patton <b-patton@ti.com> Organization: Texas Instruments Newsgroups:...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
14
by: Etu | last post by:
Hi, I have a string: string c = "'abc' \"cde\", 'mno' \"xyz\","; how can I use the c.Replace(???, ???) method to have this string: "'abc' "cde", 'mno' "xyz"," that is, all the...
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
8
by: Warren Moxley | last post by:
Hi there, i've been searching for a C String search and replace function. I need to find all occurrences of " " in a char* array, and replace them with another char, I know how to do this in...
5
by: herman | last post by:
How can I replace all occurrences of a character with another character in std string? For example, I want to replace '/' with '+' in my std::string I have looked at the replace() method in...
6
by: Gabriel | last post by:
Hello, I do this : s = Environment.CurrentDirectory; s = s.Replace("\\", @"\"); Environment.CurrentDirectiry return a path like this C:\\....\\....\\..... I'd like replace the \\ by \, but...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
6
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.