# Parsing text with regular expression

**URL:** <https://www.ruby-forum.com/t/parsing-text-with-regular-expression/93239>\
**Category:** Ruby\
**Created:** [April 29, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/parsing-text-with-regular-expression/93239 "2007-04-29T22:42:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/parsing-text-with-regular-expression/93239/1 "2007-04-29T22:42:28Z")

</div>

Hi  
I am writing a class that parses text. It checks each word and counts  
how many times they occur in the text. It also checks for ‘special’  
words, that being words that are capitalized, all upper case or in mixed  
case, and ads a flag to those words and checks that the words that are  
not special fulfill a certain length requirement. The information is  
stored in a hash like this:

{‘word’ =\> {:count =\> 1, :special =\> false}, ‘other\_word’ =\> {:count=\>  
3, :special =\> true}}

Everything is working fine so far. The thing I am struggling to  
implement though is the following:  
I want to be able to check the context the ‘special’ words are in to see  
if a capitalized special word maybe only is capitalized because it is  
the first word in a new sentence or something like that.

I thought I could check by looking for something like this:

text =~ /[[:punct:]]\s?WORD\_I\_AM\_LOOKING\_FOR/  
and if I got something else than 0 as a result it would mean that the  
word is in the beginning of a sentence. But how do I insert a variable  
into the regular expression? Or is there a different much cleverer way  
to do this sort of check?

Currently I am scanning for each word like this:

\_inn.scan(/\w{2,}[-\w]?/i) do |word|  
…  
end

and then doing the checking of the words inside that iterator.

Hope you have understood my problem and that you can point me in the  
right direction.

best regards  
Sebastian

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/parsing-text-with-regular-expression/93239/2 "2007-04-29T22:52:36Z")

</div>

Sebastian probst Eide wrote:

> I thought I could check by looking for something like this:
> 
> text =~ /[[:punct:]]\s?WORD\_I\_AM\_LOOKING\_FOR/  
> and if I got something else than 0 as a result it would mean that the  
> word is in the beginning of a sentence. But how do I insert a variable  
> into the regular expression?  
> Use #{}, like this

word = “hello”

test =~ /[[:punct:]]\s?#{word}/

“word” can be any regular expression.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 29, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/parsing-text-with-regular-expression/93239/3 "2007-04-29T22:54:59Z")

</div>

Timothy H. wrote:

> Sebastian probst Eide wrote:
> 
> > I thought I could check by looking for something like this:
> > 
> > text =~ /[[:punct:]]\s?WORD\_I\_AM\_LOOKING\_FOR/  
> > and if I got something else than 0 as a result it would mean that the  
> > word is in the beginning of a sentence. But how do I insert a variable  
> > into the regular expression?  
> > Use #{}, like this
> 
> word = “hello”
> 
> test =~ /[[:punct:]]\s?#{word}/
> 
> “word” can be any regular expression.

Huh… that was the first thing I tried… must have done something else  
wrong too in the same expression because it didn’t work… I’ll try  
again.  
Thanks Timothy

Sebastian
