473,769 Members | 5,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace character

Hello,

I have a text as follows:

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

"My email is so*******@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
Nov 20 '08 #2
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
Nov 20 '08 #3
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
Nov 20 '08 #4
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 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+)+)@(?<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
Nov 20 '08 #5
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 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+)+)@(?<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
Nov 21 '08 #6

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

Similar topics

1
2194
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: alt.comp.lang.learn.c-c++ I'm trying to remove the \n from a string. If I just simply locate teh char and replace it with \0 then the destructor should only delete up to the \0 and leave 1 char unrecovered.
5
2638
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* replace){ char* result; size_t l = strlen(source), r = strlen(replace), i; int number_of_replaces = 0; for(i = 0; i < l; i++){ if(source == search)
19
78833
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 find one.
14
3278
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 backslashes are removed.
18
4632
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 has some different characters in it that I am trying to remove. The small a with two dots over it and the small y with two dots over it. Here is my code so far to remove the small y: Private Sub Button2_Click(ByVal sender As System.Object, ByVal...
8
22125
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 managed environments, but i'm still learning C, does anyone have a quicky function handy? or point me in the right direction on how to acccomplish this? your help is greatly appreciated. Also a note, this is running on a embedded device, so i can't...
5
25527
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 the string class, it does not replace all occurrences of a character with another character. http://www.cppreference.com/cppstring/index.html
6
6805
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 the code I use not work. Any idea ? Best Regards,
10
18638
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 parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The problem is I don't know the character to replace, just must replace the character at a known...
6
3096
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 am having a problem loading the XML correctly into my app. Here is the code: ==================================== Dim sPaymentXML as String
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.