perl - force matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl - force matching
# 1  
Old 05-17-2010
perl - force matching

for below perl code, if without ?, will print test, otherwise will print null.
i know it is due to greedy perl regexp matching that eat out test by previous .*, i also know there should be a way to force perl to match if can match, can anyone help me to figure it out or lead me to the right direction?

Code:
my $str="This is a testing string";
print $1 if $str=~/This.*(test)?.*$/;

# 2  
Old 05-17-2010
Probably misunderstanding you here, but what's wrong with:

Code:
print $1 if $str=~/This.*(test).*$/;

That matches a "This", followed by zero-or-more of anything, followed by "test", followed by zero-or-more of anything, up to the end of the line.

On my machine it just prints "test". Could you post desired output, pls.?
# 3  
Old 05-17-2010
Code:
Problem:-
You have a pattern with a greedy quantifier like *, +, ?, or {}, and you want to stop it from being greedy.
A classic case of this is the naïve substitution to remove tags from HTML. Although it looks appealing,
s#<TT>.*</TT>##gsi, actually deletes everything from the first open TT tag through the last closing one
This would turn "Even <TT>vi</TT> can edit <TT>troff</TT> effectively." into "Even
effectively", completely changing the meaning of the sentence!

Code:
Solution
Replace the offending greedy quantifier with the corresponding non-greedy version. That is, change *, +, ?,
and {} into *?, +?, ??, and {}?, respectively.


Code:
perl -wle '
$str="This is a testing string";
print $1 if $str=~/This.*?(test).*$/;
' infile.txt

Hope I answer you question.
SmilieSmilieSmilie
# 4  
Old 05-17-2010
Cite Reference Material Please

Quote:
Originally Posted by ahmad.diab
Problem:-
You have a pattern with a greedy quantifier like *, +, ?, or {}, and you want to stop it from being greedy.
A classic case of this is the naïve substitution to remove tags from HTML. Although it looks appealing,
s#<TT>.*</TT>##gsi, actually deletes everything from the first open TT tag through the last closing one
This would turn "Even <TT>vi</TT> can edit <TT>troff</TT> effectively." into "Even
effectively", completely changing the meaning of the sentence!

Hope I answer you question.
I hope Tom and Nat answered your question.

--Tom Christiansen, Nathan Torkington, "The Perl Cookbook, 2nd Edition" O'Reilly & Associates, May 1999, Chapter 6, Section 15 SmilieSmilieSmilie
# 5  
Old 05-17-2010
it doesn't matter if you know this info or not but it is great if you know where to look for your question in books

Code:
By Tom Christiansen & Nathan Torkington; ISBN 1-56592-243-3, 794 pages.
First Edition, August 1998.

SmilieSmilieSmilie
SmilieSmilieSmilie
# 6  
Old 05-17-2010
Right my mistake, I had incorrectly cited the source material you did not cite as the source of your post, as being the 2nd Edition of the Perl Cookbook. It is indeed the First Edition where you got that from. Thank you for the correction.
# 7  
Old 05-19-2010
thanks, but still not word

hi all,

thanks very much for your reply.

probably i did not express myself clearly.

please refer to below code and expected output.

actually what i need is:
1> if no match, just let greedy * eat out the following
2> if match, should not eat out

code:
Code:
my @arr=("This is a testing string","This is a tesing string");
map {print $1,"\n" if /This.*?(test)?.*$/} @arr;
print "--\n";

expect output:
Code:
test (here there do have test, so have to force math)
(here no match, so just nothing)
--


Last edited by summer_cherry; 05-23-2010 at 10:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern matching in Perl

Hi, I have a list of IP, eg : 192.168.0.15 192.168.0.24 192.168.2.110 192.168.2.200 And I would like the shortest pattern who match with '192.168.0' and '192.168.2' (without the last dot and number). (7 Replies)
Discussion started by: X-Or
7 Replies

2. Shell Programming and Scripting

Need help with perl pattern matching

My log file looks as given below, its actually a huge file around 1 GB and these are some of the line: conn=5368758 op=10628050 msgId=64 - RESULT err=0 tag=101 nentries=1 etime=0 conn=7462122 op=-1 msgId=-1 - fd=247 slot=247 LDAPS connection from 10.13.18.12:37645 to 10.18.6.45 conn=7462122... (5 Replies)
Discussion started by: sags007_99
5 Replies

3. Shell Programming and Scripting

perl: array matching

Hi, I have two files like this file 1: xxtcgtatccgaggga cgcgcgggggagg jjsjjjjsjjjdtcgtat aaaaaaacccaaan ggtcgtatffaadda gggctggalllslllssdkk file 2: tcgtat gctggaI want to 1) match each element of file2 to each element of file1. 2) delete all the matched alphabets and subsequent... (3 Replies)
Discussion started by: polsum
3 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 pattern matching!!

Hi experts, I have many occurances of the following headers in a file. I need to grep for the word changed/inserted in the header, calculate the difference between the two numbers and list the count incrementally. Headers in a file look like this: ------------------- ---------------------... (6 Replies)
Discussion started by: nmattam
6 Replies

6. Shell Programming and Scripting

Perl -Pattern Matching help..!

Hi, I got doubt in Pattern matching, could you tell me how the following differs in action ?? if ( $line1==/$line2/ ) if ( $line1=~/$line2/ ) if ( $line1=~m/$line2/) What is the significance of '~' in matching. Thanks in advance CoolBhai (5 Replies)
Discussion started by: coolbhai
5 Replies

7. Shell Programming and Scripting

Matching words in Perl

Hi, I have an array in which one column can contain any statement. From multiple rows of that column I want to match the statement like "Execution Started." If that row contains "Execution started." then only I have to fetch other data of other columns of that particular row. I dont want... (2 Replies)
Discussion started by: monika
2 Replies

8. Shell Programming and Scripting

Perl Pattern Matching !!! Help

Hello I got the below one from in one of this forums For Ex: Loading File System Networking in nature now i need to extract the patterns between the words File and Networking : i.e. sample output: System cmd used : cat <file> | sed 's/.*File //' | sed 's/Closing.*$//' Actually... (0 Replies)
Discussion started by: maxmave
0 Replies

9. Shell Programming and Scripting

perl pattern matching

hi i am trying to get digits inside brackes from file , whose structure is defined below CREATE TABLE TELM (SOC_NO CHAR (3) NOT NULL, TXN_AMOUNT NUMBER (17,3) SIGN_ON_TIME CHAR (8) TELLER_APP_LIMIT NUMBER (17,3) FIL01 ... (2 Replies)
Discussion started by: zedex
2 Replies

10. Shell Programming and Scripting

force use of specific Perl library

I am calling a Perl script that uses Net::FTP; from Oracle's Concurrent Request Manager and it fails becuase Oracle's version of Perl doesn't include the NET::FTP library's. The AIX box the script runs on has Perl installed with the needed library's, does anyone know how I can force my script to... (1 Reply)
Discussion started by: methos
1 Replies
Login or Register to Ask a Question