# How can I do this with regex?

**URL:** <https://www.ruby-forum.com/t/how-can-i-do-this-with-regex/175032>\
**Category:** Ruby\
**Created:** [September 6, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/how-can-i-do-this-with-regex/175032 "2009-09-06T17:18:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 6, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/how-can-i-do-this-with-regex/175032/1 "2009-09-06T17:18:11Z")

</div>

I want to grep Delphi “class name” and “reference classes” in a delphi  
class file.  
How can I do that with regex?

example delphi class like this;

[http://exampledelphi.com/?p=62](http://exampledelphi.com/?p=62)

any idea?

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 6, 2009, 8:17pm UTC](https://www.ruby-forum.com/t/how-can-i-do-this-with-regex/175032/2 "2009-09-06T20:17:32Z")

</div>

Ahmet K. wrote:

> I want to grep Delphi “class name” and “reference classes” in a delphi  
> class file.  
> How can I do that with regex?
> 
> example delphi class like this;
> 
> [http://exampledelphi.com/?p=62](http://exampledelphi.com/?p=62)
> 
> any idea?

What do you want to do? Grep through a text for a specific string?

“string”.grep //

I dont think that many will understand delphi here (isnt delphi  
graphical btw? not sure how this compares to ruby…) so it is best to  
try to formulate the question more ruby specific (or neutral so that  
others have an easier time to understand it)

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 7, 2009, 12:53am UTC](https://www.ruby-forum.com/t/how-can-i-do-this-with-regex/175032/3 "2009-09-07T00:53:22Z")

</div>

Hello Ahmet:

I’m not a Ruby guru or anything, I still do most of my coding in Delphi  
(I  
love Ruby but it’s too slow). However, here are a few hints to help you  
do  
what I think your wanting to do (note: each assumes you’ve filled  
current\_line with a line of text from your Delphi code):

1. Returns true if current\_line contains something like " TMyClass =  
class  
(TObject)" :  
 → current\_line.match(/=\sclass\b/i)

2. Returns the class name (e.g. TMyClass) from current\_line found with  
the  
regex from #1:  
 → current\_line.match(/^\s_T(\w_)/)

3. Returns true if the current\_line variable starts with and only  
contains  
the word “implementation” which will be important for you to know when  
your  
about to enter the implementation section:  
 → current\_line.match(/^\s_implementation\s_$/i)

4. You’ll need to keep track of the class names you’ve captured and use  
a  
regex in the implementation section to find when you’ve found a usage of  
a  
class. I’ll leave that to you to figure out. However, testing for a  
word  
(i.e. the class name) on it’s own using regex word boundaries with  
something  
like this should help:  
 → current\_line.match(/\bTMyClass\b/i)

5. You can use variables (e.g. each class name you’re searching for)  
within  
a regex by wrapping it in #{} like this:  
 → current\_line.match(/#{myvariable}/)

6. This Ruby feature seems to be very usefull for code parsing, it tells  
you  
that you’re currently between lines that contains “begin” and “end”.  
 → current\_line.match(/begin/i) … current\_line.match(/end/i)

7. Google is your friend; here are a few links (including an online  
regex  
evaluator to play with) I found usefull…  
[http://rubular.com/](http://rubular.com/)  
[http://www.rubyxp.com/](http://www.rubyxp.com/)  
[http://www.rubyist.net/~slagell/ruby/regexp.html](http://www.rubyist.net/~slagell/ruby/regexp.html)  
[http://codelikezell.com/how-to-use-ruby-variables-in-regular-expressions/](http://codelikezell.com/how-to-use-ruby-variables-in-regular-expressions/)  
[Ruby Regexp Class - Regular Expressions in Ruby](http://www.regular-expressions.info/ruby.html)

Good Luck,

Michael

“Ahmet K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 7, 2009, 1:00am UTC](https://www.ruby-forum.com/t/how-can-i-do-this-with-regex/175032/4 "2009-09-07T01:00:59Z")

</div>

Hello Ahmet:

I’m not a Ruby guru or anything, I still do most of my coding in Delphi  
(I  
love Ruby but it’s too slow). However, here are a few hints to help you  
do  
what I think your wanting to do (note: each assumes you’ve filled  
current\_line with a line of text from your Delphi code):

1. Returns true if current\_line contains something like " TMyClass =  
class  
(TObject)" :  
 → current\_line.match(/=\sclass\b/i)

2. Returns the class name (e.g. TMyClass) from current\_line found with  
the  
regex from #1:  
 → current\_line.match(/^\s_T(\w_)/)

3. Returns true if the current\_line variable starts with and only  
contains  
the word “implementation” which will be important for you to know when  
your  
about to enter the implementation section:  
 → current\_line.match(/^\s_implementation\s_$/i)

4. You’ll need to keep track of the class names you’ve captured and use  
a  
regex in the implementation section to find when you’ve found a usage of  
a  
class. I’ll leave that to you to figure out. However, testing for a  
word  
(i.e. the class name) on it’s own using regex word boundaries with  
something  
like this should help:  
 → current\_line.match(/\bTMyClass\b/i)

5. You can use variables (e.g. each class name you’re searching for)  
within  
a regex by wrapping it in #{} like this:  
 → current\_line.match(/#{myvariable}/)

6. This Ruby feature seems to be very usefull for code parsing, it tells  
you  
that you’re currently between lines that contains “begin” and “end”.  
 → current\_line.match(/begin/i) … current\_line.match(/end/i)

7. Google is your friend; here are a few links (including an online  
regex  
evaluator to play with) I found usefull…  
[http://rubular.com/](http://rubular.com/)  
[http://www.rubyxp.com/](http://www.rubyxp.com/)  
[http://www.rubyist.net/~slagell/ruby/regexp.html](http://www.rubyist.net/~slagell/ruby/regexp.html)  
[http://codelikezell.com/how-to-use-ruby-variables-in-regular-expressions/](http://codelikezell.com/how-to-use-ruby-variables-in-regular-expressions/)  
[Ruby Regexp Class - Regular Expressions in Ruby](http://www.regular-expressions.info/ruby.html)

Good Luck,

Michael

“Ahmet K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 7, 2009, 5:18am UTC](https://www.ruby-forum.com/t/how-can-i-do-this-with-regex/175032/5 "2009-09-07T05:18:49Z")

</div>

Michael, Thank you very very much.  
It is very usefull.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 7, 2009, 7:13am UTC](https://www.ruby-forum.com/t/how-can-i-do-this-with-regex/175032/6 "2009-09-07T07:13:07Z")

</div>

On Sunday 06 September 2009 06:00:06 pm Michael B. wrote:

> I’m not a Ruby guru or anything, I still do most of my coding in Delphi (I  
> love Ruby but it’s too slow).

I’m curious – do you have any numbers to back this up? Or any real  
applications you tried in Ruby, and found it was too slow?

Or is it that Delphi is somehow faster to develop in?

I’m not saying you’re wrong, but I do most of my coding in Ruby, because  
I  
rarely find places it’s too slow. I’m always curious what those edge  
cases are  
that need another tool.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 9, 2009, 5:37am UTC](https://www.ruby-forum.com/t/how-can-i-do-this-with-regex/175032/7 "2009-09-09T05:37:07Z")

</div>

“David M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> I’m not saying you’re wrong, but I do most of my coding in Ruby, because I  
> rarely find places it’s too slow. I’m always curious what those edge cases  
> are  
> that need another tool.

Hello David:

Regarding code development speed: For complex web applications (I use  
Delphi’s built-in IntraWeb framework) and many Windows GUI applications  
I  
find Delphi much faster to work with. The language and DB data  
providers  
are archaic by Ruby / Python standards but the integration between the  
IDE,  
language, built in components and the host windowing environment saves  
so  
much time for most of the projects I’ve worked on (e.g. database  
applications, graphics engines, data visualization, data generation /  
simulation, general utilities / home projects). I’ve tried Ruby R.  
before Delphi IntraWeb, it was easy to build simple stuff but as the  
visual  
and functional complexity went up it was just easier to use Delphi  
IntraWeb.  
I didn’t know IntraWeb at the time but it’s WYSIWYG design environment  
for  
the web pages which was tightly integrated with the Delphi code was so  
much  
faster to develop with… not like the “code generator / run it and see  
what  
it looks like” feel I got from Rails.

Regarding execution speed: I love the true OOP and metaprogramming of  
Ruby  
but it’s so slow executing compared to a compiled language like Java,  
C++ or  
Delphi. A while ago I wrote a simple test to calculate prime numbers  
with  
Delphi, Python and Ruby (using the same brute force approach in each)  
and  
found:

Delphi (v6.0) = 1.52s  
Delphi (v2009) = 3.41s = 2.24 times slower than Delphi v6  
Python (v2.8) = 54.25s = 35.69 times slower than Delphi v6  
Ruby (v1.8.6) = 121.82s = 80.14 times slower than Delphi v6  
Ruby (v1.9.1) = 53.70s = 35.33 times slower than Delphi v6

using a Pentium 4, 3.0 Ghz, 1GB Memory, Windows XP SP2. I did the same  
test  
on SUSE Linux 10.2 and found Ruby to be about 30% faster than the  
results on  
Windows (note: Python was 20% slower on SUSE Linux so compilers and  
memory  
management appear to make a big difference).

That’s not the only program I’ve written in both Delphi and Ruby. I  
ported  
a Ruby XMLTV viewer (which deals with a little over a gigabyte of data)  
to  
Delphi and the Delphi program ran 2.5 times faster (i.e. 63 seconds vs  
160  
seconds). The Delphi program was at a disadvantage because the Delphi  
component I used applied an in-memory XML DOM approach vs the Ruby  
component’s XML SAX approach (i.e. more memory overhead and validation  
for  
the Delphi program).

The OpenGL graphics engine I wrote in Ruby was very fast in the  
beginning  
(2100 FPS) for a simple 3D scenes because most of the work was being  
done on  
the GPU but as the engine grew to include reflection, shadows, AI (all  
of  
which require more passes per frame on the Ruby side) then it really  
slowed  
down (i.e. 60 FPS and dropping). I’m still working on this project and  
hope  
to find better ways to use Ruby but found that Delphi didn’t slow down  
as  
quickly in similar situations.

I also starting writing a source code translator (which is why I had  
some  
advice for this thread) which relied heavily on Ruby gsubs. It’s a lot  
faster than I expected (maybe %80 of the speed of an equivalent Delphi  
program) but that’s likely because the gsubs are doing most of the work.

Ruby really gets killed by looping through stuff. For my graphics  
engines  
that means multi-passes on 3d objects for the diffuse, reflection and  
shadow  
passes. For XML parsing that means going through all the records in the  
XML  
file and parsing the tags/containers. For the pirme number calculator  
that  
was simple looping. Ruby does great if you can let the behind the  
scenes  
optimized functions (or external C/C++ libraries) do most of the work.

I"m not saying I used the best designs for either my Ruby or Delphi  
programs  
but for all the tricks and neat stuff I can do in Ruby there are limits  
to  
the time I’m willing to invest in Ruby (i.e to write external routines  
or  
redesign things to use Ruby smarter) to get the speed I need.

I’ve seen the “Ruby / Python / Lua / etc is fast enough for me”  
discussion  
on various forums several times and it usually depends on the kind of  
thing  
you’re trying to use the language for. In my case I’m using Ruby for  
some  
of the worst case senarios that you can throw at an interpretive / VM  
language.

However, to Ruby’s credit because of it’s dynamic nature and true OOP I  
can  
do some things in Ruby for my game engine that I’ve always wished I  
could do  
in Delphi so I haven’t given up hope on it yet. The speed is the only  
real  
issue.

Did I answer you question?

Michael

p.s. Please forgive any poor grammer, etc… it’s a long response and I  
don’t want to re-read it to many times.
