Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular expression

Posted on 2000-02-24
2
Medium Priority
?
239 Views
Last Modified: 2010-03-05
I have a Txt file wich have a line like this:

{ELEM1,ELEM2,ELEM3}

This line can have as many ELEM? as I want, so I have to read a diferent number of elements each line.

For example:
Line1: {ELEM1}
Line2: {ELEM2,ELEM3}
Line3: {ELEM1,ELEM2,ELEM3}


How can I make a Regular expression so I can put in a Array the diferents elements.

Example:

The Regular expression....
Array[0]=ELEM1
Array[1]=ELEM2
 ......
 




0
Comment
Question by:Pampa
2 Comments
 
LVL 2

Accepted Solution

by:
mattrope earned 400 total points
ID: 2554280
Try this (assuming the line is stored in $line:

if ($line =~ /\{(.*?)\}/) {
  @array = split(',',$1);
}

Hope this is what you were looking for.
0
 

Expert Comment

by:KishoreS78
ID: 2554578
i hope that i have understood ur question.ur ultimate aim is to read lines and populate in to array which
has elem1 as first element,ELEM2 as second and so on
this should work though this is not the most effective way of doing it to my knowledge.u can make it more effective

while($line = <FD>) {#read line from file
 
@temp =  split(/,/$line) ;
for($i=0;$i<($#temp+1);$++) {
 if($temp[$i] =` /^E.*?(\d)/{
   $array[($1-1)] = $temp[$i]
  }
}
}
print @array;
i think u could do evenwithout splitting .i havent tried it out
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question