Regular Expression issue

A

anon1m0us

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

}
 
B

brian.corbin

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
 

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
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top