Solved

Regular expression

Posted on 2000-02-24
2
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
mattrope earned 100 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

717 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