Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Regex lookahead, lookbehind

by choroba (Cardinal)
on Feb 23, 2017 at 10:04 UTC ( [id://1182618]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Regex lookahead, lookbehind
in thread Regex lookahead, lookbehind

I don't understand your requirements. It now seems to me none of the examples is correct, as TEST and TEST_test don't contain at least one digit, and TEST-1 doesn't contain at least one underscore. Changing the dash to underscore in the last makes it pass:
#!/usr/bin/perl use warnings; use strict; while (<DATA>) { if (my ($alnum, $constrained) = /^foo # "foo" _ # followed by "underscore" (?:bar|test) # followed by "bar|test" (bar or test) _ # followed by "underscore" ((?:\w+\.)+?) # followed by alhpnum string with # dot at end (like foo10.1.1.1) \1? # followed by one or zero occurences # of same alhpnum string (like # foo10.1.1.1) followed by dot (\w{1,10}) # followed by 1-10 characters, # containing at least one digit and # one letter and underscore (e.g. # TEST_test, TEST_test2, # TEST1_test2) \.txt$ # ends with .txt /x) { print "<$alnum | $constrained> $. ok\n" if 3 == grep $constrained =~ $_, qr/_/, qr/\d/, qr/[[:alpha:]]/; } } __DATA__ foo_bar_foo10.1.1.1.TEST.txt foo_test_foo10.1.1.1.foo10.1.1.1.TEST_test.txt foo_test_foo10.1.1.1.foo10.1.1.1.txt foo_test_foo10.1.1.1.foo10.1.1.1.TEST-1.txt foo_test_foo10.1.1.1.foo10.1.1.1.TEST_1.txt

Note that I changed the quantifier in the first capturing group to frugal to avoid matching both occurrences of $alnum as one without repetition. Nevertheless, in the third case, the whole substring foo10.1.1.1.foo10.1.1. corresponds to $alnum, it's not repeated, and $constrained is just 1 .

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^4: Regex lookahead, lookbehind
by rahulme81 (Sexton) on Feb 23, 2017 at 11:40 UTC
    Hi choroba ... sorry for the confusion with the post. below is what I am doing.
    foo_bar_foo10.1.1.1.foo10.1.1.1.txt foo_bar_foo10.1.1.1.foo10.1.1.1.FOO-1.txt foo_bar_foo10.1.1.1.foo10.1.1.1.test_FOO1.txt foo_bar_foo10.1.1.1.foo10.1.1.1.test_FOO_abc_mon.txt

    Above are the files which I need to query with regular expression. Out of which I need to pick only file2, file3, file4

    and move them to some other directory. file1 should remain untouched.

    Similarly sometime another set of files is as below

    foo_foo_foo10.1.1.1.TEST.failed foo_foo_foo10.1.1.1.test_FOO1.failed

    Out of which I need to pick only file2 and move them to some other directory. file1 should remain untouched.

    As of now I have separte regex to filter and picking the desired files with grep -v.

    Thanks
      > Out of which I need to pick only file2, file3, file4

      But why? Do you just want to skip the shortest name? If so, use length instead of a regex. Otherwise, try to explain how you select the files to stay or move.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1182618]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found