By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,533 Members | 1,497 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,533 IT Pros & Developers. It's quick & easy.

Regex - copy pattern from one string to another

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.