Regex - copy pattern from one string to another

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • redamazon200@googlemail.com

    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 = "1111AAAA111111 AA";
    string str2 = "11110000000011 11";

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

    TIA

    Rohit

  • Jon Shemitz

    #2
    Re: Regex - copy pattern from one string to another

    redamazon200@go oglemail.com wrote:
    >
    I am looking for a way to copy a pattern (letter 'A' in the following
    example) to another string.
    >
    string str1 = "1111AAAA111111 AA";
    string str2 = "11110000000011 11";
    >
    After the copy str2 becomes "1111AAAA000011 AA".
    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

    What you need to know.

    Comment

    • Mark R. Dawson

      #3
      RE: Regex - copy pattern from one string to another

      Hi Rohit,
      as well as using regular expressions another possibility is using a
      StringBuilder:

      //PREDICATE: str1,str2 are always the same length
      string str1 = "1111AAAA111111 AA";
      string str2 = "11110000000011 11";

      StringBuilder s1 = new StringBuilder(s tr1);
      StringBuilder s2 = new StringBuilder(s tr2);
      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.
      --



      "redamazon200@g ooglemail.com" wrote:
      I am looking for a way to copy a pattern (letter 'A' in the following
      example) to another string.
      >
      string str1 = "1111AAAA111111 AA";
      string str2 = "11110000000011 11";
      >
      After the copy str2 becomes "1111AAAA000011 AA".
      Can this is done efficiently using Regex?
      >
      TIA
      >
      Rohit
      >
      >

      Comment

      • redamazon200@googlemail.com

        #4
        Re: Regex - copy pattern from one string to another

        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 = "1111AAAA111111 AA";
        string str2 = "11110000000011 11";
        >
        StringBuilder s1 = new StringBuilder(s tr1);
        StringBuilder s2 = new StringBuilder(s tr2);
        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.
        --

        >
        >
        "redamazon200@g ooglemail.com" wrote:
        >
        I am looking for a way to copy a pattern (letter 'A' in the following
        example) to another string.

        string str1 = "1111AAAA111111 AA";
        string str2 = "11110000000011 11";

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

        TIA

        Rohit

        Comment

        • Lucian Wischik

          #5
          Re: Regex - copy pattern from one string to another

          redamazon200@go oglemail.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+eff icient only if it turns out to be a
          bottleneck in practic.

          --
          Lucian

          Comment

          • Kevin Spencer

            #6
            Re: Regex - copy pattern from one string to another

            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 = "1111AAAA111111 AA";
            string str2 = "11110000000011 11";

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

            --
            HTH,

            Kevin Spencer
            Microsoft MVP
            Logostician
            Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.


            There is a madness to my method.

            <redamazon200@g ooglemail.comwr ote in message
            news:1165572458 .105097.46250@j 44g2000cwa.goog legroups.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
            >StringBuilde r:
            >>
            > //PREDICATE: str1,str2 are always the same length
            > string str1 = "1111AAAA111111 AA";
            > string str2 = "11110000000011 11";
            >>
            > StringBuilder s1 = new StringBuilder(s tr1);
            > StringBuilder s2 = new StringBuilder(s tr2);
            > 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
            >>
            >>
            >"redamazon200@ googlemail.com" wrote:
            >>
            I am looking for a way to copy a pattern (letter 'A' in the following
            example) to another string.
            >
            string str1 = "1111AAAA111111 AA";
            string str2 = "11110000000011 11";
            >
            After the copy str2 becomes "1111AAAA000011 AA".
            Can this is done efficiently using Regex?
            >
            TIA
            >
            Rohit
            >
            >
            >

            Comment

            Working...