Regex: Get Class Name

A

Ahmet Kilic

This a C++ class name,
const std::string DRAPBase::CLASS_NAME = "DRAPBase";
I am writing like this
classname = text.scan(/::CLASS_NAME = (.*?)\s+";/)
but it is not true.
how can I get the class name with regex?
 
B

Brian Candler

Ahmet said:
This a C++ class name,
const std::string DRAPBase::CLASS_NAME = "DRAPBase";
I am writing like this
classname = text.scan(/::CLASS_NAME = (.*?)\s+";/)
but it is not true.

You're matching `::CLASS_NAME = ' followed by zero or more characters
followed by one or more space characters followed by ";

It's the "one or more space characters" which doesn't match. Maybe you
meant \S which means non-space character? e.g.

classname = text.scan /::CLASS_NAME = "(\S+?)";/
 
R

Robert Klemme

2009/8/31 Brian Candler said:
You're matching `::CLASS_NAME =3D ' followed by zero or more characters
followed by one or more space characters followed by ";

It's the "one or more space characters" which doesn't match. Maybe you
meant \S which means non-space character? e.g.

classname =3D text.scan /::CLASS_NAME =3D "(\S+?)";/

I'd probably do

/::CLASS_NAME\s*=3D\s*"([^"]+)"/

Note to OP: (.*?) is a particular awful construction especially with a
variable length match pattern afterwards. As long as there is at
least one space (.*?) will always match the empty string in this case:

irb(main):010:0> 5.times {|i| p /(.*?)\s+/ =3D~ (" " * i), $1, $&, '---'}
nil
nil
nil
"---"
0
""
" "
"---"
0
""
" "
"---"
0
""
" "
"---"
0
""
" "
"---"
=3D> 5
irb(main):011:0>

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Ryan Davis

I'd probably do

/::CLASS_NAME\s*=\s*"([^"]+)"/

I'd generally agree with that, but as I told the OP in IRC, in this
case I think it is better to think outside the box:

name = src[/(\w+)::CLASS_NAME\s*=/, 1]

Since that is the real name, it makes more sense to me.
 
A

Ahmet Kilic

Ryan said:
I'd probably do

/::CLASS_NAME\s*=\s*"([^"]+)"/

I'd generally agree with that, but as I told the OP in IRC, in this
case I think it is better to think outside the box:

name = src[/(\w+)::CLASS_NAME\s*=/, 1]

Since that is the real name, it makes more sense to me.

thank you very much.
 
R

Robert Klemme

2009/8/31 Ryan Davis said:
I'd probably do

/::CLASS_NAME\s*=\s*"([^"]+)"/

I'd generally agree with that, but as I told the OP in IRC, in this case I
think it is better to think outside the box:

name = src[/(\w+)::CLASS_NAME\s*=/, 1]

Since that is the real name, it makes more sense to me.

Ah, that wasn't obvious from the original posting.

Btw, this looks suspiciously like someone is trying to model dynamic
language features in C++. How simple that would be in Ruby

name = any_class.name

:)

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top