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 8195181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:02:54+00:00 2026-06-07T05:02:54+00:00

I am trying to write a regular expression in PERL that will pick the

  • 0

I am trying to write a regular expression in PERL that will pick the shortest match. I have written one that will identify many matches, but I need to be able to pick the shortest of all the matches.

Let say I have the following text:

$text = "blah BEG blah blah blah END blah blah BEG blah END blah BEG blah blah END";

I can use this regular expression to identify the three cases that begin with BEG and end with END without either BEG or END being between BEG and END.

/(BEG(?:(?!BEG|END).)*END)/

It captures the three cases.

BEG blah blah blah END
BEG blah END 
BEG blah blah END

I want to match only the second one since it is shortest of the three.

I have considered pulling all matches to an array and identifying the shortest element of the array.

Is there an easier way to incorporate this into the regular expression?

Thanks in advance for your thoughts and help!

  • 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-07T05:02:55+00:00Added an answer on June 7, 2026 at 5:02 am
    use List::Util qw( reduce );
    
    my $shortest =
       reduce { length($a) < length($b) ? $a : $b }
          /(BEG(?:(?!BEG|END).)*END)/s;
    

    It can be done entirely in a Perl regular expression (with the help of embedded Perl code), but it would be silly unless you absolutely require that.

    my ($shortest) = /
       ( BEG (?:(?!BEG|END).)* END )
       (?!
          .*
          ( BEG (?:(?!BEG|END).)* END )
          (?(?{ length($2) >= length($1) })(*FAIL))
       )
    /sx;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a python regular expression that will match both these URLs:
I am trying to write a regular expression that will match a string that
So I am trying to write a regular expression that will match a defined
In java, I'm trying to write a regular expression that will match a unit
I'm trying to write a regular expression (via Autohotkey's RegExReplace function) that will enforce
I'm trying to write a regular expression that will validate that user input is
I am trying to write an regular expression in c# that will check a
I'm trying to write a regular expression that will essentially return true if string
I'm trying to write a regular expression in flash that will find the word
I'm trying to write a regular expression that will check so a given string

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.