# Regular expression

**URL:** <https://www.ruby-forum.com/t/regular-expression/125985>\
**Category:** Ruby\
**Created:** [January 14, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/regular-expression/125985 "2008-01-14T14:07:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/1 "2008-01-14T14:07:38Z")

</div>

hey

im lookiing to be able to write a regular expressions which deals with  
10 letter strings which end in either “ed” or “ing”

any help would be much appreciated

thanks

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/2 "2008-01-14T14:19:57Z")

</div>

2008/1/14, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> im lookiing to be able to write a regular expressions which deals with  
> 10 letter strings which end in either “ed” or “ing”
> 
> any help would be much appreciated

Doesn’t sound too difficult. What did you came up with so far?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/3 "2008-01-14T14:43:17Z")

</div>

hey

only a very basic start im afraid

^([a-zA-Z]{10})$

i.e  
ten letter word (upper or lower case)  
basically i need to get it now so it only deals with words with the  
suffix’s a mentioned

any pointers?

thanks

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/4 "2008-01-14T14:48:23Z")

</div>

2008/1/14, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> any pointers?

You can do either

if /^\w{7}(?:\wed|ing)$/i =~ str

# process match

end

or

if str.length == 10 && /(?:ed|ing)$/i =~ str

# process match

end

There are of course more alternatives… 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 2:58pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/5 "2008-01-14T14:58:43Z")

</div>

i appreciate the help

however, im still having problems

im using the regex coach to see if i can get a match but im being  
unsuccessful

im using

^\w{10}(?:\ed|ing)$

with the target string controlled

yet not getting a match  
can you see why?

thanks

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/6 "2008-01-14T15:06:42Z")

</div>

2008/1/14, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> with the target string controlled
> 
> yet not getting a match  
> can you see why?

Yes.

robert

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/7 "2008-01-14T15:10:20Z")

</div>

thank you.

would be be able to offer any suggestions as to where im going wrong?

thanks

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/8 "2008-01-14T15:24:32Z")

</div>

thank you.

what ive basically got now is

\w{7}(ed|ing)

was basically works for ing but only matches a 9 letter word for ed

there must be a better way to do it  
ive been playing around to with no luck

any suggestions would be greatly appreciated

thanks

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/9 "2008-01-14T15:15:34Z")

</div>

On 1/14/08, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> with the target string controlled
> 
> yet not getting a match  
> can you see why?

That’s looking for 10 word characters FOLLOWED by either ed or ing.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/10 "2008-01-14T16:05:05Z")

</div>

The “\w{7}” matches with 7 chars, so to match all your expression it  
have to have 9 chars ending with ed and 10 chars ending with ing.

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/11 "2008-01-14T16:35:47Z")

</div>

2008/1/14, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> thank you.
> 
> what ive basically got now is
> 
> \w{7}(ed|ing)
> 
> was basically works for ing but only matches a 9 letter word for ed
> 
> there must be a better way to do it  
> ive been playing around to with no luck

Maybe you should rather _read_ what people write if they take the time  
to help you.

> any suggestions would be greatly appreciated

Have been made already.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/12 "2008-01-14T16:00:57Z")

</div>

On Mon, Jan 14, 2008 at 11:24:30PM +0900, Johnathan S. wrote:

> thank you.
> 
> what ive basically got now is
> 
> \w{7}(ed|ing)

\w{7}(?:\wed|ing)

= 7 word characters followed by either a word character and the string  
“ed” or the string “ing”.

> was basically works for ing but only matches a 9 letter word for ed

Because your version tells it to match on 7 word characters followed by  
“ed” or “ing”.

Dan

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 14, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/regular-expression/125985/13 "2008-01-14T17:31:04Z")

</div>

On Jan 14, 6:10 am, Johnathan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thank you.
> 
> would be be able to offer any suggestions as to where im going wrong?

You didn’t copy/paste exactly what Robert wrote. You’re missing a  
piece. Hint: \e is not meaningful in a (Ruby) regexp.

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 15, 2008, 9:24am UTC](https://www.ruby-forum.com/t/regular-expression/125985/14 "2008-01-15T09:24:56Z")

</div>

2008/1/15, Paul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> You can also expand the alternation all the way through.  
> For instance, imagine: (?:\w{X}ed|\w{Y}ing)  
> for suitable numeric values of X and Y and applied anchoring.

But this is less efficient with NFA regexp engines because it has to  
do more backtracking. Actually, I believe it is most efficient to  
have the longest trailing portion first, e.g. /^\w{7}(?:ing|\wed)$/i.  
If you are interested you can play a bit with  
[The Regex Coach - interactive regular expressions](http://www.weitz.de/regex-coach/).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![johnsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d9b06d/32.png) [@johnsmith](https://www.ruby-forum.com/u/johnsmith)\
**Post date:** [January 15, 2008, 12:25am UTC](https://www.ruby-forum.com/t/regular-expression/125985/15 "2008-01-15T00:25:25Z")

</div>

You can also expand the alternation all the way through.  
For instance, imagine: (?:\w{X}ed|\w{Y}ing)  
for suitable numeric values of X and Y and applied anchoring.
