Perl expression help...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl expression help...
# 1  
Old 05-20-2011
Perl expression help...

Pls. advise what is the meaning of below expression...

if ($filename =~ /x([a-zA-Z0-9_\.]+), [0-9]+ bytes/)



--with the 1st enclosed parenthesis...
/x([a-zA-Z0-9_\.]+)

this will match/filter all the filenames with any character, number, or underscore, and /x modifiers means I can put whitespace characters (like spaces, tabs, and newlines) into the expression. ??

-- but not sure with this [0-9]+ bytes/



many thanks... Smilie
# 2  
Old 05-20-2011
Code:
if ($filename =~ /x([a-zA-Z0-9_\.]+), [0-9]+ bytes/)

I suspect it is used to extract a file name from a report as it sets $1 to "name.txt" if the value "xname.txt, 4096 bytes" is found in he variable $filename

So how does it do that, if the value contained in the $filename variable matches the following pattern then do whatever is in the block following this expression.
Code:
Pattern Breakdown
/x # an x
([a-zA-Z0-9_\.]+) # followed by any word character or a literal dot one or more times,
# the brackets indicate that we should store the matched value
# it can be accessed in the containing block as $1
#This clause could be more neatly written as ([\w\.]+)
,\s[0-9]+#followed by a comma a space and more than one digit
#The \s representation of a space is required 
#if you allow comments in a regex
\sbytes #Followed by another space and the word bytes
/x # Added by me, allow comments in the regex.

The curious thing is that you can do this with any complex regex to help the person who has to maintain your code later, but I rarely see it used Smilie

To find out more about how Perl's regular expressions work

Last edited by Skrynesaver; 05-20-2011 at 03:30 AM.. Reason: Added link to the documentaion .
This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 05-20-2011
There is a small slip in the explanation:
Quote:
,\s[0-9]+#followed by a comma a space and more than one digit
Should be
Quote:
,\s[0-9]+#followed by a comma, a whitespace character and at least one digit
These 2 Users Gave Thanks to mirni For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

reg expression in perl

how to uniquely match each of the words seperated by / in perl ${REP_PATH}/FUNCTIONAL/wide1c_1.0V/max/qor.rpt https://www.unix.com/images/misc/progress.gif (5 Replies)
Discussion started by: dll_fpga
5 Replies

2. Shell Programming and Scripting

reg expression in perl

./GEN_SCR.pl -f ${REP_PATH}/FUNCTIONAL/wide1c_1.0V/max/qor.rpt -o ${REP_PATH}/FUNCTIONAL/GEN2_wide1c_1.0V_max.csv where GEN_SCR.pl is as below...i need to check whether max or min is coming in the argument to the script ...how to do this? ${REP_PATH}/FUNCTIONAL/wide1c_1.0V/max/qor.rpt ... (0 Replies)
Discussion started by: dll_fpga
0 Replies

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

Perl expression

I need to calculate the number of pipes '|' coming in my expression. My expression consists of alphanumeric text including special charactres seperated by colon ':' W1:W2|W3:W4|W5:W6 .. likewise I need to calculate number of pipes '|' occuring in them eg aaaa : bbbb | cc@34% :... (3 Replies)
Discussion started by: r_t_1601
3 Replies

7. 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

8. Shell Programming and Scripting

perl regular expression

Dear all, I have a simple issue on a perl regular expression. I want to get the characters in red from the next lines : POWER_key LEFT_key RIGHT_key OK_key DOWN_key and so on... Thanks in advance for reply. Ludo (1 Reply)
Discussion started by: lsaas
1 Replies

9. 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

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