Perl regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl regular expression
# 1  
Old 08-30-2012
Perl regular expression

Hi ,

I have the below array
Code:
my @actionText = ("delivered to governor on 21/23/3345" , "deliver jllj" , "ram 2345/43");

When i am trying to grep the contents of array and if mathced substituting with the digitis or some date format from the element like below

Code:
my @action = grep { $_ =~ s/delivered to governor on (.*)|ram/$2/ } @actionText;
print "@action"."\n";

the output is
--------------
Code:
21/23/3345 2345/43

how is this 2345/43 getting stored in $action[1] array as i have not used () after ram in regular expression...

Please help me to understand this

Thanks
Ragilla


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-30-2012 at 05:47 AM.. Reason: code tags
# 2  
Old 08-30-2012
I'm not terribly familiar with perl, but assuming the s/... works like normal sed: I would guess it's matching the entire 'ram' element @actiontext[2], but you're only replacing the 'ram' part with $2 (or actually nothing, for that match) - leaving the remainder of that element to be put into @action[1].
# 3  
Old 08-30-2012
perl

Hi,

Here you are grepping the array element which contain string 'delivered to governor on' or 'ram' and do some substitution. So you wont get the array elements which dont have the matching string.
I think, you need all the array elements and do the substitition in some of the array elements.

Code:
my @actionText = ("delivered to governor on 21/23/3345" , "deliver jllj" , "ram 2345/43");
my @action = map{$_=~ s/delivered to governor on |ram//;$_;} @actionText;
print "@action\n";

This is what you are looking for ..?
It will remove the string 'delivered to governor on ' or 'ram' before assigning all the array elements from @actionText to @action.

Cheers,
Ranga Smilie
# 4  
Old 09-02-2012
Hi Ranga,

you misunderstood my question, i know the difference between map and grep.

i wanted to substitute "delivered to governor on 21/23/3345" with 21/23/3354 and "ram 2345/43" with "2345/43", so i substituted with $2 in delivered to governor on 21/23/3345, but the string when matched ram... how it is substituting "2345/43" even though i dint group that in regular expression.

Regards
Sridhar
# 5  
Old 09-02-2012
Quote:
Originally Posted by ragilla
...
the output is
--------------
Code:
21/23/3345 2345/43

how is this 2345/43 getting stored in $action[1] array as i have not used () after ram in regular expression...
Your question should be - why is "21/23/3345" showing up in the array @action.
The string " 2345/43" has all the more reason to be there because you substituted "ram" by nothing, thereby chopping it off.

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 6  
Old 09-03-2012
Quote:
Originally Posted by ragilla
Hi Ranga,

you misunderstood my question, i know the difference between map and grep.

i wanted to substitute "delivered to governor on 21/23/3345" with 21/23/3354 and "ram 2345/43" with "2345/43", so i substituted with $2 in delivered to governor on 21/23/3345, but the string when matched ram... how it is substituting "2345/43" even though i dint group that in regular expression.

Regards
Sridhar
You are chopping the texts 'delivered to governor on ' or 'ram ' with $2. Here $2 deosn't have any value hence it substitute with empty string.
durden_tyler has been stated above clearly. Thanks mate Smilie

What you are trying to do with this..?

Cheers,
Ranga Smilie
# 7  
Old 09-10-2012
Hi tyler_durden,

Thanks for your reply, i missed that.
I have one more question. i have code like below , i need to get the text between certain patterns , suppose i have below array

Code:
use strict;
use warnings;
@a = ("my asdf bye", "ram lslkdjfsalf raj");

@a = grep { $_ =~ s/my (.*) bye|ram (.*) raj/$1$2/ } @a;

as first pattern match the required text will be in $1 and As second pattern match the required text will be in $2.

but when first pattern match $2 is empty it will give warning like this
Use of uninitialized value $2 in concatenation (.) or string at

So how to avoid those warnings .. please help me..

thanks in advance
Ragilla
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

2. Shell Programming and Scripting

Perl regular expression help!

Hi I am doing something basic like... if ($stringvariable =~ /have not typed/) I have a little problem because the 'not' in the expression gets highlighted as a kind of a '!'..what am I supposed to do in this situation? Thank you ---------- Post updated at 03:24 PM ----------... (1 Reply)
Discussion started by: vas28r13
1 Replies

3. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

4. Shell Programming and Scripting

Perl regular expression and %

Could you help me with this please. This regular expression seems to match for the wrong input #!/usr/bin/perl my $inputtext = "W1a$%XXX"; if($inputtext =~ m/+X+/) { print "matches\n"; } The problem seems to be %. if inputtext is W1a$XXX, the regex doesnot match.... (5 Replies)
Discussion started by: suppandi7
5 Replies

5. Shell Programming and Scripting

Perl Regular Expression

Hello, I am trying to use perl LWP module to read and get a specfic URL page. The issue is that the URL ends with the data and time and time is not consistent it changes all the time. if anyone could help me how to write a regular expressin that would work in the LWP::UserAgent get function to... (0 Replies)
Discussion started by: bataf
0 Replies

6. Shell Programming and Scripting

Regular expression in Perl

Hi, I need and expression for a word like abc_xyz_ykklm The expresion should indicate that the word starts with abc and end with ykklm but does not contain xyz string in the middle. Example: abc_tmn_ykklm is ok and abc_xyz_ykklm is not Ok. Please help. Regards. (1 Reply)
Discussion started by: asth
1 Replies

7. Shell Programming and Scripting

PERL regular expression

Hello all, I need to match the red expressions in the following lines : MACRO_P+P-_scrambledServices_REM_PRC30.xml MACRO_P+P-_scrambledServices_REM_RS636.xml MACRO_P+P-_scrambledServices_REM_RS535.xml and so on... Can anyone give me a PERL regular expression to match those characters ? ... (5 Replies)
Discussion started by: lsaas
5 Replies

8. Shell Programming and Scripting

regular expression in perl

hi, i want to extract the sessionID from this line. QnA Session Id : here the output should be-- QnA_SessionID=128589 Thanks NT (3 Replies)
Discussion started by: namishtiwari
3 Replies

9. Shell Programming and Scripting

perl regular expression

letz say that my file has 7 records with only one field. So my file has: 11111111 000000000000000 1111 aaaabbbccc 1111111222000000 aaaaaaaa zz All i need is: 1. when the field has a repetition of the same instance(a-z or 0-9), i would consideer it to be invalid.... (1 Reply)
Discussion started by: helengoldman
1 Replies

10. Shell Programming and Scripting

Regular expression help in perl

Hi all, I am trying to match a multi line string and return the matching string in one line. Here is the perl code that I wrote: #!/usr/bin/perl my $str='<title>My title</title>'; if ($str =~ /(<title>)(+)(<\/title>)/ ){ print "$2\n"; } It returns : My title I want the... (3 Replies)
Discussion started by: sdubey
3 Replies
Login or Register to Ask a Question