Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:15:08+00:00 2026-06-14T16:15:08+00:00

Unfortunately, I’m not a regex expert, so I need a little help. I’m looking

  • 0

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:

  1. with elements which contain specific $phrase but we exclude those strigns which simultaneously start with specific $speakerId AND end with one of specified id’s (for instance $id1 or $id2)

  2. with elements which have specific $speakerId and $phrase but 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:15:09+00:00Added an answer on June 14, 2026 at 4:15 pm

    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:

    my @all_entries      = readdir(...);
    my @matching_entries = ();
    
    foreach my $entry (@all_entries) {
    
        # split file name
        next unless /^(\d+)-(.*?)-(\d+).txt$/;
        my ($sid, $phrase, $id) = ($1, $2, $3);
    
        # filter
        next unless $sid eq "foo";
        next unless $id == 42 or $phrase eq "bar";
        # more readable filter rules
    
        # match
        push @matching_entries, $entry;
    }
    
    # do something with @matching_entries
    

    If you really want to express something that complex in a grep list transformation, you could write code like this:

    my @matching_entries = grep {
    
        /^(\d)-(.*?)-(\d+).txt$/
        and $1 eq "foo"
        and ($3 == 42 or $phrase eq "bar")
        # and so on
    
    } readdir(...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

unfortunately my settings do not get saved. please help If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Unfortunately cabal-dev ghci does not work in this project, i get an error: Loading
unfortunately I have not found anything yet, maybe anyone could help me or give
Unfortunately, Lucida Grande does not have an italic variant and I need one. My
Unfortunately searching for 'Sybase Post' doesn't get me the answers I'm looking for. I
Unfortunately, Google didn't help me much: I'm looking for some information regarding the execution
Unfortunately, my question is not as simple as keeping track of two windows created
Unfortunately, I often find I need to reboot to wipe some unhealthy Xcode state.
Unfortunately their Wiki is down for maintenance and the web is not being helpful.
Unfortunately I haven't coded Java for about five years and I absolutely can not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.