Unfortunately, I’m not a regex expert, so I need a little help.
I’m looking for the solution how to grep an array of strings to get two lists of strings which do not start (1) or end (2) with the specific substring.
Let’s assume we have an array with strings matching to the following rule:
[speakerId]-[phrase]-[id].txt
i.e.
10-phraseone-10.txt 11-phraseone-3.txt 1-phraseone-2.txt
2-phraseone-1.txt 3-phraseone-1.txt 4-phraseone-1.txt
5-phraseone-3.txt 6-phraseone-2.txt 7-phraseone-2.txt
8-phraseone-10.txt 9-phraseone-2.txt 10-phrasetwo-1.txt
11-phrasetwo-1.txt 1-phrasetwo-1.txt 2-phrasetwo-1.txt
3-phrasetwo-1.txt 4-phrasetwo-1.txt 5-phrasetwo-1.txt
6-phrasetwo-3.txt 7-phrasetwo-10.txt 8-phrasetwo-1.txt
9-phrasetwo-1.txt 10-phrasethree-10.txt 11-phrasethree-3.txt
1-phrasethree-1.txt 2-phrasethree-11.txt 3-phrasethree-1.txt
4-phrasethree-3.txt 5-phrasethree-1.txt 6-phrasethree-3.txt
7-phrasethree-1.txt 8-phrasethree-1.txt 9-phrasethree-1.txt
Let’s introduce variables:
$speakerId$phrase$id1,$id2
I would like to grep a list and obtain an array:
-
with elements which contain specific
$phrasebut we exclude those strigns which simultaneously start with specific$speakerIdAND end with one of specified id’s (for instance$id1or$id2) -
with elements which have specific
$speakerIdand$phrasebut do NOT contain one of specific ids at the end (warning: remember to not exclude the 10 or 11 for$id=1, etc.)
Maybe someone coulde use the following code to write the solution:
@AllEntries = readdir(INPUTDIR);
@Result1 = grep(/blablablahere/, @AllEntries);
@Result2 = grep(/anotherblablabla/, @AllEntries);
closedir(INPUTDIR);
I like the approach with pure regular expressions using negative lookaheads and -behinds. However, it’s a little bit hard to read. Maybe code like this could be more self-explanatory. It uses standard perl idioms that are readable like english in some cases:
If you really want to express something that complex in a
greplist transformation, you could write code like this: