# Regular Expression issue

**URL:** <https://www.ruby-forum.com/t/regular-expression-issue/84237>\
**Category:** Ruby\
**Created:** [February 2, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/regular-expression-issue/84237 "2007-02-02T17:05:50Z")\
**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:** [February 2, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/regular-expression-issue/84237/1 "2007-02-02T17:05:50Z")

</div>

Hi; I created a program that needs to parse through multiple files  
looking for users and groups. The users are listed with Employee  
Number and Name and then Groups. The format is as follows:

C008658 Smith,John  
C009525 Smith, Jane  
C014259 Doe, Joe  
C044888 Doe, Jack  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*(yes, the stars  
are part of the file)

Outsource Access Control Sheet - Finance  
Project Manager: Gilmore, Happy (0083234)  
Date of first approval: Not approved  
Date of last update: First Update  
Accounts:  
Type: LAN  
Groups: Yes-Some-Type-of\_Group

I need to capture, the employee ID, Name, and Groups. My Current  
program is not working to well. Here is ONE of the version of the  
program I am working on to get it to work.

Cemployee=Array.new  
CemployeeID=Array.new  
CemployeeName=Array.new  
counter=0  
CemployeeS=" "  
File.open(“D:\Finance.txt”).each { |user|  
while user.scan(/^[\*]+/)

```
CemployeeID[counter]=user.scan(/^[C]\d+/)
puts CemployeeID[counter]
CemployeeName[counter]=user.scan(/\s\w+[,]\w+/)
puts CemployeeName[counter]
counter+=1

```

end

}

---

<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:** [February 2, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/regular-expression-issue/84237/2 "2007-02-02T18:41:01Z")

</div>

Instead of capturing each data element one at a time with separate  
user.scan’s, it’s easier to create a match object from a single  
regular expression that contains match groups. You can then access  
the results of the match group by using $1, $2, etc.  
Try this:

Cemployee=Array.new  
CemployeeID=Array.new  
CemployeeName=Array.new  
counter=0  
CemployeeS=" "  
File.open(“D:\finance.txt”).each { |user|  
m = /^([C]\d+)\s(\w+,\s?\w+)/.match(user)  
if m  
CemployeeID[counter]=$1  
CemployeeName[counter]=$2  
puts “Name: #{CemployeeName[counter]} ID:  
#{CemployeeID[counter]}”  
counter+=1  
end  
}

Hope that helps-

Brian
