[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular Expression issue

anon1m0us

2/2/2007 4:00:00 PM

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

}

1 Answer

brian.corbin

2/2/2007 5:40:00 PM

0

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