# Replace a very long string with gsub get regexp error

**URL:** <https://www.ruby-forum.com/t/replace-a-very-long-string-with-gsub-get-regexp-error/179016>\
**Category:** Ruby\
**Created:** [November 15, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/replace-a-very-long-string-with-gsub-get-regexp-error/179016 "2009-11-15T21:44:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 15, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/replace-a-very-long-string-with-gsub-get-regexp-error/179016/1 "2009-11-15T21:44:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 15, 2009, 10:50pm UTC](https://www.ruby-forum.com/t/replace-a-very-long-string-with-gsub-get-regexp-error/179016/2 "2009-11-15T22:50:33Z")

</div>

On 15.11.2009 21:44, Ahmad A. 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

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 15, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/replace-a-very-long-string-with-gsub-get-regexp-error/179016/3 "2009-11-15T23:47:31Z")

</div>

Robert K. wrote:

> On 15.11.2009 21:44, Ahmad A. 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

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 16, 2009, 11:14am UTC](https://www.ruby-forum.com/t/replace-a-very-long-string-with-gsub-get-regexp-error/179016/4 "2009-11-16T11:14:55Z")

</div>

Ahmad A. 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

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 17, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/replace-a-very-long-string-with-gsub-get-regexp-error/179016/5 "2009-11-17T20:35:26Z")

</div>

Robert K. wrote:

> On 15.11.2009 23:47, Ahmad A. 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

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [November 16, 2009, 8:40am UTC](https://www.ruby-forum.com/t/replace-a-very-long-string-with-gsub-get-regexp-error/179016/6 "2009-11-16T08:40:30Z")

</div>

On 15.11.2009 23:47, Ahmad A. 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
