# Redefining word boundaries?

**URL:** <https://www.ruby-forum.com/t/redefining-word-boundaries/107042>\
**Category:** Ruby\
**Created:** [June 17, 2007, 3:12am UTC](https://www.ruby-forum.com/t/redefining-word-boundaries/107042 "2007-06-17T03:12:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmb-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jmb-d](https://www.ruby-forum.com/u/jmb-d)\
**Post date:** [June 17, 2007, 3:12am UTC](https://www.ruby-forum.com/t/redefining-word-boundaries/107042/1 "2007-06-17T03:12:16Z")

</div>

Is it possible to redefine (temporarily) the meaning of \b in RegExp  
situations?

Here’s the scenario: I’ve got some data that looks something like,  
“ABCD.E”, which I stuff into an array as I encounter each new  
instance. Eventually, I will join(", ") these together having  
surrounded each of them with single quotes, for use in an SQL “IN  
(…)” clause.

At first, I tried doing this with gsub(/\b/, “’”), but found out the  
hard way that the period was wreaking havoc with my intended results,  
and I got this: ‘ABCD’.‘E’ instead.

My next attempt was to do it by hand, like this: gsub(/^/, “’”).gsub(/  
$/, “’”). This worked, but seems a bit like a hack.

So my question is whether there’s any way to temporarily declare that  
a period is NOT to be considered a word boundary.

Thanks, in advance,  
jmb-d

---

<div class="post-metadata">

**Author:** ![jmb-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jmb-d](https://www.ruby-forum.com/u/jmb-d)\
**Post date:** [June 17, 2007, 6:05am UTC](https://www.ruby-forum.com/t/redefining-word-boundaries/107042/2 "2007-06-17T06:05:26Z")

</div>

On 6/17/07, jmb-d [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My next attempt was to do it by hand, like this: gsub(/^/, “'”).gsub(/  
> $/, “'”). This worked, but seems a bit like a hack.

Does this give you the proper format?

arr = [“ABCD.E”,“FGHI.J”,“KLMN.O”]  
puts arr.map{|x|“‘#{x}’”}.join(“,”)

Harry

–

A Look into Japanese Ruby List in English  
[http://www.kakueki.com/](http://www.kakueki.com/)

---

<div class="post-metadata">

**Author:** ![jmb-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jmb-d](https://www.ruby-forum.com/u/jmb-d)\
**Post date:** [June 17, 2007, 7:45am UTC](https://www.ruby-forum.com/t/redefining-word-boundaries/107042/3 "2007-06-17T07:45:33Z")

</div>

On Jun 16, 9:06 pm, jmb-d [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it possible to redefine (temporarily) the meaning of \b in RegExp  
> situations?

I think the only way to do this would be to redefine all the  
circumstances in which a \b could occur yourself. For instance the 6  
cases that I can think of are: ^\w, \W\w, \s\w, \w$, \w\W, \w\s. This  
is a pain as you can see here I’ve done four and it’s very ugly:

> “ABCD.E foo FG.HI”.gsub(/^(\w)/, “'\1”).gsub(/(\w)$/, “\1’”).gsub(/(\w)(\s)/, “\1’\2”).gsub(/(\s)(\w)/, “\1’\2”)  
> =\> “‘ABCD.E’ ‘foo’ ‘FG.HI’”

Alternatively you could pre and post process by replacing “.” with  
something that doesn’t break the boundary “zyzyzy” and then replace  
back with “.” when you’re done inserting the ticks:

> “ABCD.E foo FG.HI”.gsub(/./, “zyzyzy”).gsub(/\b/, “'”).gsub(/zyzyzy/, “.”)  
> =\> “‘ABCD.E’ ‘foo’ ‘FG.HI’”

Neither are very nice solutions.

- Byron

---

<div class="post-metadata">

**Author:** ![jmb-d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jmb-d](https://www.ruby-forum.com/u/jmb-d)\
**Post date:** [June 18, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/redefining-word-boundaries/107042/4 "2007-06-18T14:35:58Z")

</div>

On Jun 17, 12:03 am, “Harry K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does this give you the proper format?
> 
> arr = [“ABCD.E”,“FGHI.J”,“KLMN.O”]  
> puts arr.map{|x|“‘#{x}’”}.join(“,”)

Harry –

Yes, that’s the result I’m looking for.

Thanks!
