# UTF in Regexp

**URL:** <https://www.ruby-forum.com/t/utf-in-regexp/84380>\
**Category:** Ruby\
**Created:** [February 3, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/utf-in-regexp/84380 "2007-02-03T20:15:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![abu-mats](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@abu-mats](https://www.ruby-forum.com/u/abu-mats)\
**Post date:** [February 3, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/utf-in-regexp/84380/1 "2007-02-03T20:15:42Z")

</div>

I am sorrrrrry, but I am banging my head against this, and can’t seem to  
find the answer!

Text gets displayed in an input field in a web page with “  
prepended and ” appended to the string (needs to be inside the  
string otherwise it looks funny). The user edits it, and when it comes  
back to the (Rails) backend, the new string with (possibly) these quotes  
attached comes back, but in unicode.

So the string possibly starts with UTF “ and possibly ends with  
UTF ”

I want to do a regexp removal. Here is what works (but I am embarrased):

ldquo = ‘123’; ldquo[0] = 226; ldquo[1] = 128; ldquo[2] = 156  
rdquo = ‘123’; rdquo[0] = 226; rdquo[1] = 128; rdquo[2] = 157  
string.gsub!(/(\A#{ldquo}|#{rdquo}\Z)/,’’)

There must be a better way.

Abu Mats al-Nemsi

---

<div class="post-metadata">

**Author:** ![abu-mats](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@abu-mats](https://www.ruby-forum.com/u/abu-mats)\
**Post date:** [February 3, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/utf-in-regexp/84380/2 "2007-02-03T21:12:12Z")

</div>

On 2/3/07, Wido M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So the string possibly starts with UTF “ and possibly ends with  
> UTF ”
> 
> I want to do a regexp removal. Here is what works (but I am embarrased):
> 
> ldquo = ‘123’; ldquo[0] = 226; ldquo[1] = 128; ldquo[2] = 156  
> rdquo = ‘123’; rdquo[0] = 226; rdquo[1] = 128; rdquo[2] = 157  
> string.gsub!(/(\A#{ldquo}|#{rdquo}\Z)/,‘’)
> 
> There must be a better way.

1. it’s possible to insert the chars directly, either in octal (226 =  
“\342”) or hexa (226= “\xe2”)

string.gsub!(\A\xe2\x80\x9c|\xe2\x80\9d\Z/,")

1. | has low priority, so your regex is equal to /(\Alquo)|(rquo\z)/.  
the correct one is (notice the non-capturing group (?:…)

string.gsub!(\A(?:\xe2\x80\x9c|\xe2\x80\9d)\Z/,")

1. there’s iconv library that will convert things for you.
