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.