473,473 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regex - copy pattern from one string to another

I am looking for a way to copy a pattern (letter 'A' in the following
example) to another string.

string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";

After the copy str2 becomes "1111AAAA000011AA".
Can this is done efficiently using Regex?

TIA

Rohit

Dec 8 '06 #1
5 2789
re**********@googlemail.com wrote:
>
I am looking for a way to copy a pattern (letter 'A' in the following
example) to another string.

string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";

After the copy str2 becomes "1111AAAA000011AA".
Can this is done efficiently using Regex?
I take it you don't always want to overwrite str2 with A - that you
want to be able to take a regex match on string1, and overwrite
string2 with the parts of string1 that matched in string1. Yes, this
can be an efficient operation. You'll have to decide what to do if
string1 has a match at positions I though J, and string2 is shorter
than I or J, but:

The Regex.Match method returns a Match object. If the Success property
is true, the Match instance has an Index into the target, and a match
Length. So, you could replace that part of another string with the
Match.Value. (You will want to use a StringBuilder: copy the head,
copy the first overwrite, copy the next substring, do the next
overwrite, and so on.)

You can call Match.NextMatch until you get a Match that's not a
Success, or you can call Regex.Matches to get a MatchCollection.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Dec 8 '06 #2
Hi Rohit,
as well as using regular expressions another possibility is using a
StringBuilder:

//PREDICATE: str1,str2 are always the same length
string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";

StringBuilder s1 = new StringBuilder(str1);
StringBuilder s2 = new StringBuilder(str2);
for (int i = 0; i < s1.Length; ++i)
{
if (s1[i] == 'A')
{
s2[i] = 'A';
}
}

Which is very clear (in my opinion clearer than using a regular expression)
and efficient.

Mark.
--
http://www.markdawson.org
"re**********@googlemail.com" wrote:
I am looking for a way to copy a pattern (letter 'A' in the following
example) to another string.

string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";

After the copy str2 becomes "1111AAAA000011AA".
Can this is done efficiently using Regex?

TIA

Rohit

Dec 8 '06 #3
Thanks Jon and Mark for your responses.

Clarifying two things - yes, the strings are of same length. And all I
have to worry about is location(s) of 'A' in the first string and have
another string (say str3) that is same as str2 but has 'A' s in it at
the same locations as str1. I could also overwrite str2 and slot in the
'A's based on str1.

I can see Mark's solution - clean and simple. But really I would like
to avoid looping through each and every character - Imagine if my
string size is 1000 and I am doing this operation thousands of times!

I am also looking at bit-wise operations and see whether it is possible
to do a bit operation to get what I want.

The problem is still open so I would appreciate comments.

Thanks.

RP


Mark R. Dawson wrote:
Hi Rohit,
as well as using regular expressions another possibility is using a
StringBuilder:

//PREDICATE: str1,str2 are always the same length
string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";

StringBuilder s1 = new StringBuilder(str1);
StringBuilder s2 = new StringBuilder(str2);
for (int i = 0; i < s1.Length; ++i)
{
if (s1[i] == 'A')
{
s2[i] = 'A';
}
}

Which is very clear (in my opinion clearer than using a regular expression)
and efficient.

Mark.
--
http://www.markdawson.org
"re**********@googlemail.com" wrote:
I am looking for a way to copy a pattern (letter 'A' in the following
example) to another string.

string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";

After the copy str2 becomes "1111AAAA000011AA".
Can this is done efficiently using Regex?

TIA

Rohit
Dec 8 '06 #4
re**********@googlemail.com wrote:
>I can see Mark's solution - clean and simple. But really I would like
to avoid looping through each and every character - Imagine if my
string size is 1000 and I am doing this operation thousands of times!
I am also looking at bit-wise operations and see whether it is possible
to do a bit operation to get what I want.
One possibility is to "compile" the pattern-string into a list of
indexes where "A" occurs. Then, iterate through this list and set 'A'
in the targt. This would be much more efficient if the pattern-string
changes rarely and is sparse.

You could also be more efficient by performing the operation on DWORD
boundaries rather than on individual characters, but I don't know how
this is done in .net.

Bitmasks? The only way you could get a REAL performance increase is by
considering your two strings as bitmaps. The pattern bitmap would be
compiled into two bitmaps, traditionally "AND" followed by "XOR". This
would let you use the hardware graphics accelerator to do your job,
and would be orders of magnitude faster than anything else you could
achieve, but a heck of a lot more convoluted.

As always the best answer is to use the simplest code possible. And
make it more complicated+efficient only if it turns out to be a
bottleneck in practic.

--
Lucian
Dec 8 '06 #5
If the strings are of the same length, it's a simple problem to solve. A
string is an array of char. So, all you need to do is loop through the first
string, and if the char at position i in the first string is 'A' set the
char at position i in the second string to 'A'. There is no reallocation of
memory involved. Example:

string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";

for (int i = 0; i < str1.Length; i++)
if (str1[i] == 'A') str2[i] = 'A';

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

There is a madness to my method.

<re**********@googlemail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...
Thanks Jon and Mark for your responses.

Clarifying two things - yes, the strings are of same length. And all I
have to worry about is location(s) of 'A' in the first string and have
another string (say str3) that is same as str2 but has 'A' s in it at
the same locations as str1. I could also overwrite str2 and slot in the
'A's based on str1.

I can see Mark's solution - clean and simple. But really I would like
to avoid looping through each and every character - Imagine if my
string size is 1000 and I am doing this operation thousands of times!

I am also looking at bit-wise operations and see whether it is possible
to do a bit operation to get what I want.

The problem is still open so I would appreciate comments.

Thanks.

RP


Mark R. Dawson wrote:
>Hi Rohit,
as well as using regular expressions another possibility is using a
StringBuilder:

//PREDICATE: str1,str2 are always the same length
string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";

StringBuilder s1 = new StringBuilder(str1);
StringBuilder s2 = new StringBuilder(str2);
for (int i = 0; i < s1.Length; ++i)
{
if (s1[i] == 'A')
{
s2[i] = 'A';
}
}

Which is very clear (in my opinion clearer than using a regular
expression)
and efficient.

Mark.
--
http://www.markdawson.org
"re**********@googlemail.com" wrote:
I am looking for a way to copy a pattern (letter 'A' in the following
example) to another string.

string str1 = "1111AAAA111111AA";
string str2 = "1111000000001111";

After the copy str2 becomes "1111AAAA000011AA".
Can this is done efficiently using Regex?

TIA

Rohit


Dec 9 '06 #6

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

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
7
by: jaylucier | last post by:
Howdy, I'm trying to break an input string into multpile pieces using a series of delimiters that start with an asterisk. Following the asterisk is a mulitple character identifier immediately...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.