[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie regex question about \w

Jeff

1/14/2006 10:15:00 PM

I'm trying to parse ruby files to find all the class definitions in the
file. For each line in the file, I thought I could use the following to
pull out the class name:

\bclass\b(\w+)\b

so then $1 would give me the class name.

But it doesn't work:

irb(main):001:0> line = "class Article < MyBaseClass"
=> "class Article < ActiveRecord::Base"
irb(main):002:0> line =~ /\bclass\b(\w+)\b/
=> nil

I think I narrowed down the problem to my use of \w, but I can't
understand why.

For extra credit, anybody know how I can make sure I can ignore comments
and quoted strings? I want to make sure I ignore these things:

if option_exists # handle class options

as well as

puts "Your are in a class by yourself"

But those are advanced... if I can just get the first one working I'll
be grateful!

Thanks,
Jeff

--
Posted via http://www.ruby-....


6 Answers

James Gray

1/14/2006 10:21:00 PM

0

On Jan 14, 2006, at 4:14 PM, Jeff Cohen wrote:

> I'm trying to parse ruby files to find all the class definitions in
> the
> file. For each line in the file, I thought I could use the
> following to
> pull out the class name:
>
> \bclass\b(\w+)\b
>
> so then $1 would give me the class name.

You're close, you just forgot to allow for some space between class
and the name. A boundary is a zero-width assertion, so it's not enough:

\bclass\s+(\w+)\b

Hope that helps.

James Edward Gray II


Jeff

1/14/2006 10:25:00 PM

0

James Gray wrote:
> You're close, you just forgot to allow for some space between class
> and the name. A boundary is a zero-width assertion, so it's not enough:

Awesome. Thanks a lot.

Jeff

--
Posted via http://www.ruby-....


Logan Capaldo

1/14/2006 10:34:00 PM

0


On Jan 14, 2006, at 5:14 PM, Jeff Cohen wrote:

> I'm trying to parse ruby files to find all the class definitions in
> the
> file. For each line in the file, I thought I could use the
> following to
> pull out the class name:
>
> \bclass\b(\w+)\b
>
> so then $1 would give me the class name.
>
> But it doesn't work:
>
> irb(main):001:0> line = "class Article < MyBaseClass"
> => "class Article < ActiveRecord::Base"
> irb(main):002:0> line =~ /\bclass\b(\w+)\b/
> => nil
>
> I think I narrowed down the problem to my use of \w, but I can't
> understand why.
>
> For extra credit, anybody know how I can make sure I can ignore
> comments
> and quoted strings? I want to make sure I ignore these things:
>
> if option_exists # handle class options
>
> as well as
>
> puts "Your are in a class by yourself"
>
> But those are advanced... if I can just get the first one working I'll
> be grateful!
>
> Thanks,
> Jeff
>
> --
> Posted via http://www.ruby-....
>

your \w is right. \b doesn't work the way you think it does though.
It doesn't consume anything, ie;

"<-- \b is just before the 'c'
c
l
a
s
s__ \b is in between the 's' and the space
<- space doesn't match \w
A
r
t
i
c
l
e

dblack

1/14/2006 10:49:00 PM

0

Simon Kröger

1/14/2006 10:49:00 PM

0

> [...]
> especially with heredocs and such. I would take a look at rdoc and see
> if you can't manipulate it to get a list of classes for you.

Maybe this is about regexps and i'm totaly off, but what about:

---------------------------------------------
before = Object.constants
require 'sqlite3' # put your file here
after = Object.constants

p(after - before)
---------------------------------------------

output:

["NKF", "Deprecated", "SQLite3", "Base64", "Kconv", "ParseDate"]


cheers

Simon


Logan Capaldo

1/14/2006 11:12:00 PM

0


On Jan 14, 2006, at 5:49 PM, Simon Kröger wrote:

> > [...]
>> especially with heredocs and such. I would take a look at rdoc
>> and see if you can't manipulate it to get a list of classes for you.
>
> Maybe this is about regexps and i'm totaly off, but what about:
>
> ---------------------------------------------
> before = Object.constants
> require 'sqlite3' # put your file here
> after = Object.constants
>
> p(after - before)
> ---------------------------------------------
>
> output:
>
> ["NKF", "Deprecated", "SQLite3", "Base64", "Kconv", "ParseDate"]
>
>
> cheers
>
> Simon
>

Clever!

before = Object.constants
require 'file'
after = Object.constants
files_classes = (after - before).select { |x| Class ===
Object.const_get(x) }