# Matching group names

**URL:** <https://www.ruby-forum.com/t/matching-group-names/92870>\
**Category:** Rails\
**Created:** [April 25, 2007, 8:23am UTC](https://www.ruby-forum.com/t/matching-group-names/92870 "2007-04-25T08:23:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [April 25, 2007, 8:23am UTC](https://www.ruby-forum.com/t/matching-group-names/92870/1 "2007-04-25T08:23:40Z")

</div>

I am trying to capture a users group. I use dsquery and the output is  
a bunch of groups starting with the words CN= group name followed by  
a comma.  
I need the group name from after CN= and before the FIRST comma. How  
can i do it. I tried spilts, scans, and matches with out much success.

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [April 26, 2007, 3:34am UTC](https://www.ruby-forum.com/t/matching-group-names/92870/2 "2007-04-26T03:34:47Z")

</div>

anon1m0us wrote:

> I am trying to capture a users group. I use dsquery and the output is  
> a bunch of groups starting with the words CN= group name followed by  
> a comma.  
> I need the group name from after CN= and before the FIRST comma. How  
> can i do it. I tried spilts, scans, and matches with out much success.

You didn’t give a complete example of what is returned by dsquery so I’m  
going to have take a guess at a working single regular expression match:

group\_name = dsquery\_str.gsub(/^CN=(._?),._$/, ‘\1’)

If the “.\*?” pattern is not working correctly you can do it the long way  
like this:

values = dsquery\_str.split(/,/)  
group\_name = values[0].gsub(/^CN=(.\*)/, ‘\1’)

That will remove any confusion the commas can cause in the pattern  
matching.

–  
Michael W.
