replace a very long string with gsub get regexp error

Discussion in 'Ruby' started by Ahmad Azizan, Nov 15, 2009.

  1. Ahmad Azizan

    Ahmad Azizan Guest

    Hello,

    I've encountered a problem to replace a very long string with gsub.
    The string length is around 80,000.

    As example,
    data contains string with length 80,000

    lala = "Replaced string"

    input = input.gsub(data, lala)

    Is there any other method for that string replacement like in this case
    beside using gsub?
    --
    Posted via http://www.ruby-forum.com/.
    Ahmad Azizan, Nov 15, 2009
    #1
    1. Advertising

  2. On 15.11.2009 21:44, Ahmad Azizan wrote:
    > Hello,
    >
    > I've encountered a problem to replace a very long string with gsub.
    > The string length is around 80,000.
    >
    > As example,
    > data contains string with length 80,000
    >
    > lala = "Replaced string"
    >
    > input = input.gsub(data, lala)
    >
    > Is there any other method for that string replacement like in this case
    > beside using gsub?


    And your problem is?

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Nov 15, 2009
    #2
    1. Advertising

  3. Ahmad Azizan

    Ahmad Azizan Guest

    Robert Klemme wrote:
    > On 15.11.2009 21:44, Ahmad Azizan wrote:
    >> input = input.gsub(data, lala)
    >>
    >> Is there any other method for that string replacement like in this case
    >> beside using gsub?

    >
    > And your problem is?
    >
    > robert


    Sorry for incomplete question.

    The problem is that, because of the very long string length, the gsub
    return a regexp error indicating that the regular expression is too
    long.

    Thank you

    --
    Posted via http://www.ruby-forum.com/.
    Ahmad Azizan, Nov 15, 2009
    #3
  4. On 15.11.2009 23:47, Ahmad Azizan wrote:
    > Robert Klemme wrote:
    >> On 15.11.2009 21:44, Ahmad Azizan wrote:
    >>> input = input.gsub(data, lala)
    >>>
    >>> Is there any other method for that string replacement like in this case
    >>> beside using gsub?

    >> And your problem is?

    >
    > Sorry for incomplete question.
    >
    > The problem is that, because of the very long string length, the gsub
    > return a regexp error indicating that the regular expression is too
    > long.


    Ahmad, it's easier for us if you present a complete example along with
    the concrete error (e.g. exception with stack trace).

    A general remark: it is very uncommon to have a regular expression whose
    text is 80k in size. So you probably rather want a different approach
    but which one is difficult to tell without having more input.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Nov 16, 2009
    #4
  5. Ahmad Azizan wrote:
    > lala = "Replaced string"
    >
    > input = input.gsub(data, lala)
    >
    > Is there any other method for that string replacement like in this case
    > beside using gsub?


    Here's one option:

    input = "aaaabbbaaaabbbaaaaabbbaaaabbbaaaa"
    data = "bb"
    lala = "zz"

    pos = 0
    while pos = input.index(data, pos)
    input[pos, data.size] = lala
    pos += lala.size
    end
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Nov 16, 2009
    #5
  6. Ahmad Azizan

    Ahmad Azizan Guest

    Robert Klemme wrote:
    > On 15.11.2009 23:47, Ahmad Azizan wrote:
    >> The problem is that, because of the very long string length, the gsub
    >> return a regexp error indicating that the regular expression is too
    >> long.

    >
    > Ahmad, it's easier for us if you present a complete example along with
    > the concrete error (e.g. exception with stack trace).
    >
    > A general remark: it is very uncommon to have a regular expression whose
    > text is 80k in size. So you probably rather want a different approach
    > but which one is difficult to tell without having more input.
    >
    > Kind regards
    >
    > robert


    Yup. I know. By showing the code, I just really don' know where to
    start.
    But after looking at candlerb's option, it is what I really needed, the
    starting and ending index of the string really help marking the certain
    content in the string.
    I can go on from that.
    Thanks so much for sharing

    --
    Posted via http://www.ruby-forum.com/.
    Ahmad Azizan, Nov 17, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,592
    Merrill & Michele
    Oct 19, 2004
  2. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    409
    jacob navia
    Oct 17, 2004
  3. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    153
    Robert Klemme
    Nov 9, 2005
  4. Craig Jolicoeur
    Replies:
    9
    Views:
    140
    7stud --
    Apr 29, 2009
  5. Joao Silva
    Replies:
    16
    Views:
    339
    7stud --
    Aug 21, 2009
Loading...

Share This Page