# Regex: Get Class Name

**URL:** <https://www.ruby-forum.com/t/regex-get-class-name/174661>\
**Category:** Ruby\
**Created:** [August 31, 2009, 8:30am UTC](https://www.ruby-forum.com/t/regex-get-class-name/174661 "2009-08-31T08:30:38Z")\
**Posts on this page:** 6\
**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:** [August 31, 2009, 8:30am UTC](https://www.ruby-forum.com/t/regex-get-class-name/174661/1 "2009-08-31T08:30:38Z")

</div>

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?

---

<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:** [August 31, 2009, 9:23am UTC](https://www.ruby-forum.com/t/regex-get-class-name/174661/2 "2009-08-31T09:23:16Z")

</div>

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

---

<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:** [August 31, 2009, 9:38am UTC](https://www.ruby-forum.com/t/regex-get-class-name/174661/3 "2009-08-31T09:38:09Z")

</div>

2009/8/31 Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<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:** [August 31, 2009, 10:16am UTC](https://www.ruby-forum.com/t/regex-get-class-name/174661/4 "2009-08-31T10:16:07Z")

</div>

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.

---

<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:** [August 31, 2009, 9:51am UTC](https://www.ruby-forum.com/t/regex-get-class-name/174661/5 "2009-08-31T09:51:37Z")

</div>

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.

---

<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:** [August 31, 2009, 10:34am UTC](https://www.ruby-forum.com/t/regex-get-class-name/174661/6 "2009-08-31T10:34:22Z")

</div>

2009/8/31 Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
