# Looking for a pattern

**URL:** <https://www.ruby-forum.com/t/looking-for-a-pattern/154825>\
**Category:** Ruby\
**Created:** [December 4, 2008, 8:30am UTC](https://www.ruby-forum.com/t/looking-for-a-pattern/154825 "2008-12-04T08:30:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 4, 2008, 8:30am UTC](https://www.ruby-forum.com/t/looking-for-a-pattern/154825/1 "2008-12-04T08:30:51Z")

</div>

I am looking for a pattern to match the last underline and its following  
string with “####”. for example,

“abc\_def\_ghi” =\> “abc\_def####”

If I use \_.\* as the pattern it will replace begin with the first  
underline. Which pattern is suite for this case?

thanks.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 4, 2008, 8:56am UTC](https://www.ruby-forum.com/t/looking-for-a-pattern/154825/2 "2008-12-04T08:56:01Z")

</div>

Zhao Yi wrote:

> I am looking for a pattern to match the last underline and its following  
> string with “####”. for example,
> 
> “abc\_def\_ghi” =\> “abc\_def####”

“abc\_def\_ghi”.match(/_[^_]\*$/)

That should do the trick, shouldn’t it?

H!

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 4, 2008, 8:59am UTC](https://www.ruby-forum.com/t/looking-for-a-pattern/154825/3 "2008-12-04T08:59:04Z")

</div>

Harald Eilertsen wrote:

> Zhao Yi wrote:
> 
> > I am looking for a pattern to match the last underline and its following  
> > string with “####”. for example,
> > 
> > “abc\_def\_ghi” =\> “abc\_def####”
> 
> “abc\_def\_ghi”.match(/_[^_]\*$/)
> 
> That should do the trick, shouldn’t it?
> 
> H!

Yes, very good. thanks.
