Regex - copy pattern from one string to another

R

redamazon200

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
 
J

Jon Shemitz

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.
 
G

Guest

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 == 'A')
{
s2 = 'A';
}
}

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

Mark.
 
R

redamazon200

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



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 == 'A')
{
s2 = 'A';
}
}

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

Mark.
--
http://www.markdawson.org


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
 
L

Lucian Wischik

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.
 
K

Kevin Spencer

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 == 'A') str2 = 'A';

--
HTH,

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

There is a madness to my method.

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



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 == 'A')
{
s2 = 'A';
}
}

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

Mark.
--
http://www.markdawson.org


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

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top