?
Solved

Perl - Regular Expression - List

Posted on 2003-03-13
5
Medium Priority
?
203 Views
Last Modified: 2010-03-05
Hi friends

I've a list of text which should be matched, and then get replaced by another list of text.  For eg., the first list will be "0,1,2,3" and the second list would be "Zero,One,Two,Three".  Is there any SINGLE pattern with which I can do this?  If so please tell me.

E.g:
Input:  This is 0 test and 1 test.
Output: This is Zero test and One test.

Thanks
Srik
0
Comment
Question by:vi_srikanth
[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
5 Comments
 
LVL 1

Expert Comment

by:zerofill
ID: 8134512
Here is an example code.
Let's say you have the two lists stored in two arrays - @first and @second

@first=("0", "1", "2");
@second=("zero", "one", "two");

$string="This is 0 test and 1 test.";
$counter=0;
foreach $element (@first)
{
$string=~ s/$first[$counter]/$second[$counter]/g;
$counter++;
}
print "$string"; #will print This is Zero test and One test.

NOTE: you must ensure that @first and @second have equal number of elements in order this to work

Good Luck
0
 
LVL 84

Accepted Solution

by:
ozo earned 80 total points
ID: 8134974
@first=(0..2);
@second=qw(zero one two);
@sub{@first}=@second;
$re=join'|',map quotemeta,@first;
$string = "This is 0 test and 1 test.";
$string =~ s/($re)/$sub{$1}/g;

0
 
LVL 4

Author Comment

by:vi_srikanth
ID: 8135300
Hi OZO,

It works precisely.  Thanks a lot.  I was looking for such a code ONLY.

But, I'm unable to figure out the meaning for the code-part:

@sub{@first}=@second;

"sub" is a hash array, but you've used "@sub" instead of "%sub".  Also, if I try the same code by changing @sub to %sub, I'm getting a compiler error.  Could u please help me?

Thanks
Srik
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8136414
@sub{@first} = @second

says to assign multiple values (@second) to multiple keys (@first) in the hash %sub.  My guess is that the @sub{} syntax was chosen to indicate that it is acting as a list and can be assigned to as such where %sub usually indicates hash "mode" where you must assign key => value pairs to it.
0
 
LVL 4

Author Comment

by:vi_srikanth
ID: 8141171
Thank You friends.

It is really weird & odd to have @sub to represent a hash array!!!  I'm really finding difficult to digest this.  Thanks again.

Bye
Srik
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

762 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