Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl regex - How to make my greedy quantifier greedier?

Reply
Thread Tools

Perl regex - How to make my greedy quantifier greedier?

 
 
cibalo
Guest
Posts: n/a
 
      05-17-2013
Hello,

I would like to try some string matching in perl as is in the title.
Let's create some testfiles for testing as follows.
$ mkdir -vp testing/dir.a/dir_b/dir-c; cd testing/dir.a/dir_b/dir-c; \
touch This_is_testing1_org.txt This-is-testing2_org.txt \
this_is_testing3_org.txt this-is-testing4_org.txt; cd

What I am looking for is the result similar to:
$ find testing -type f -name "[a-z]*\.txt"
testing/dir.a/dir_b/dir-c/this-is-testing4_org.txt
testing/dir.a/dir_b/dir-c/this_is_testing3_org.txt
I know it is more easier to find the result this way.

Now I try with perl regex as:
$ ls testing/dir.a/dir_b/dir-c/* | perl -ne '/^(.*\/)([a-z].*)$/;
print $1, " - ", $2, "\n";'
testing/dir.a/dir_b/ - dir-c/This_is_testing1_org.txt
testing/dir.a/dir_b/ - dir-c/This-is-testing2_org.txt
testing/dir.a/dir_b/dir-c/ - this_is_testing3_org.txt
testing/dir.a/dir_b/dir-c/ - this-is-testing4_org.txt
Actually, I want my leftmost greedy quantifier, (.*\/), to be so
greedier that it can prevent the first two output items from listing.

What interests me most is this:
$ ls testing/dir.a/dir_b/dir-c/* | perl -ne '/^(.*\/)([A-Z].*)$/;
print $1, " - ", $2, "\n";'
testing/dir.a/dir_b/dir-c/ - This_is_testing1_org.txt
testing/dir.a/dir_b/dir-c/ - This-is-testing2_org.txt
testing/dir.a/dir_b/dir-c/ - This-is-testing2_org.txt
testing/dir.a/dir_b/dir-c/ - This-is-testing2_org.txt
"This-is-testing2_org.txt" is repeated three times.

Can you please let me know what I'm missing?

Thank you very much in advance!!!

Best Regards,
cibalo
 
Reply With Quote
 
 
 
 
Damien Wyart
Guest
Posts: n/a
 
      05-17-2013
* cibalo <(E-Mail Removed)> in comp.lang.perl.misc:
> [...]


> Now I try with perl regex as:
> $ ls testing/dir.a/dir_b/dir-c/* | perl -ne '/^(.*\/)([a-z].*)$/;
> print $1, " - ", $2, "\n";'
> testing/dir.a/dir_b/ - dir-c/This_is_testing1_org.txt
> testing/dir.a/dir_b/ - dir-c/This-is-testing2_org.txt
> testing/dir.a/dir_b/dir-c/ - this_is_testing3_org.txt
> testing/dir.a/dir_b/dir-c/ - this-is-testing4_org.txt
> Actually, I want my leftmost greedy quantifier, (.*\/), to be so
> greedier that it can prevent the first two output items from listing.
> [...]


To answer strictly to your question, what you were looking for is '*+' ;
but this will not work in your regex: you need to exclude '/' in the
second group to match only on the filename.

You can read more on the topic (using regexes with paths and filenames)
here: http://stackoverflow.com/questions/1...y-and-filename

--
DW
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get JSON values and how to trace sessions?? webmaster@terradon.nl Python 2 04-25-2013 02:12 PM
Greedy and non greedy quantifiers Dan Kelly Ruby 4 01-19-2008 08:36 PM
Quantifier...bigger than 32766...in regex leegee@gmail.com Perl Misc 19 05-07-2006 12:25 PM
Greedy quantifier - Java 0 07-11-2005 01:34 AM
greedy v. non-greedy matching Matt Garrish Perl Misc 4 02-16-2004 03:25 PM



Advertisments