Regex: Get Class Name

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?

Ahmet K. wrote:

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+?)”;/

2009/8/31 Brian C. [email protected]:

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+?)”;/

I’d probably do

/::CLASS_NAME\s*=\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+/ =~ (" " * i), $1, $&, ‘—’}
nil
nil
nil
“—”
0
“”
" "
“—”
0
“”
" "
“—”
0
“”
" "
“—”
0
“”
" "
“—”
=> 5
irb(main):011:0>

Kind regards

robert

Ryan D. wrote:

On Aug 31, 2009, at 00:37 , Robert K. wrote:

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.

On Aug 31, 2009, at 00:37 , Robert K. wrote:

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.

2009/8/31 Ryan D. [email protected]:

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

:slight_smile:

Kind regards

robert