# Regex help, anyone?

**URL:** <https://www.ruby-forum.com/t/regex-help-anyone/147426>\
**Category:** Ruby\
**Created:** [September 1, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/regex-help-anyone/147426 "2008-09-01T18:59:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lazukars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@lazukars](https://www.ruby-forum.com/u/lazukars)\
**Post date:** [September 1, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/regex-help-anyone/147426/1 "2008-09-01T18:59:59Z")

</div>

I’m making a syntax highlighter for ruby and i just need help with a bit  
of regex.

I need to highlight function names (‘hlstring’) in strings such as:  
“def hlstring(lol, help)”  
“def hlstring (omg)”  
“def hlstring arg, arg”

Any help is much appreciated. 🙂

---

<div class="post-metadata">

**Author:** ![lazukars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@lazukars](https://www.ruby-forum.com/u/lazukars)\
**Post date:** [September 1, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/regex-help-anyone/147426/2 "2008-09-01T19:17:34Z")

</div>

On Tue Sep 2 01:54:42 2008, Ryan L. wrote:

> I’m making a syntax highlighter for ruby and i just need help with a bit  
> of regex.
> 
> I need to highlight function names

“def foo(bar, baz)”.scan(/def (\w+)/) =\> =\> [[“foo”]]

---

<div class="post-metadata">

**Author:** ![lazukars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@lazukars](https://www.ruby-forum.com/u/lazukars)\
**Post date:** [September 1, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/regex-help-anyone/147426/3 "2008-09-01T21:22:23Z")

</div>

Ryan L. wrote:

> I’m making a syntax highlighter for ruby and i just need help with a bit  
> of regex.
> 
> I need to highlight function names (‘hlstring’) in strings such as:  
> “def hlstring(lol, help)”  
> “def hlstring (omg)”  
> “def hlstring arg, arg”
> 
> Any help is much appreciated. 🙂

Here’s my stab at it.

[“def hlstring(lol, help)”,  
“def h2string (omg)”,  
“def h3string arg, arg”,  
“def a()”,  
“def a2”].each do |str|  
m = /def\s+([A-Za-z\_]\w\*)/.match(str)  
puts “#{m[1]} begins at #{m.begin(1)} ends at #{m.end(1)}”  
end
