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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:27:52+00:00 2026-05-26T12:27:52+00:00

I am looking to do a 2-step regular expression look-up in Perl, I have

  • 0

I am looking to do a 2-step regular expression look-up in Perl, I have text that looks like this:

here is some text 9337 more text AA 2214 and some 1190 more BB stuff 8790 words

I also have a hash with the following values:

%my_hash = ( 9337 => 'AA', 2214 => 'BB', 8790 => 'CC' );

Here’s what I need to do:

  1. Find a number
  2. Look up the text code for the number using my_hash
  3. Check if the text code appears within 50 characters of the identified number, and if true print the result

So the output I’m looking for is:

Found 9337, matches 'AA'
Found 2214, matches 'BB'
Found 1190, no matches
Found 8790, no matches

Here’s what I have so far:

while ( $text =~ /(\d+)(.{1,50})/g ) {
  $num = $1;
  $text_after_num = $2;
  $search_for = $my_hash{$num};
  if ( $text_after_num =~ /($search_for)/ ) {
    print "Found $num, matches $search_for\n";
  }
  else {
   print "Found $num, no matches\n";
  }

This sort of works, except that the only correct match is 9337; the code doesn’t match 2214. I think the reason is that the regular expression match on 9337 is including 50 characters after the number for the second-step match, and then when the regex engine starts again it is starting from a point after the 2214. Is there an easy way to fix this? I think the \G modifier can help me here, but I don’t quite see how.

Any suggestions or help would be great.

  • 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-05-26T12:27:52+00:00Added an answer on May 26, 2026 at 12:27 pm

    You have a problem with greediness. The 1,50 will consume as much as it can. Your regex should be /(\d+)(.+?)(?=($|\d))/

    To explain, the question mark will make the multiple match non-greedy (it will stop as soon as the next pattern is matched – the next pattern gets precedence). The ?= is a lookahead operator to say “check if the next element is a digit. If so, match but do not consume.” This allows the first digit to get picked up by the beginning of the regex and be put into the next matched pattern.

    [EDIT]
    I added an optional end value to the lookahead so that it wouldn’t die on the last match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use a Perl regular expression to match strings like this:
Just looking for the first step basic solution here that keeps the honest people
I'm new to Regular Expressions and things like that. I have only few knowledge
Say I have a SqlAlchemy model something like this: from sqlalchemy.ext.declarative import declarative_base from
I am looking for a tutorial that shows step by step how to develop
Hi and thanks for looking! Background I have a computing task that requires either
I'm looking for a step by step guide (like for dummies guide), to setup
I'm a Simple DB newb and am looking for some step-by-step instructions about how
I'm looking for a working tutorial for Rails 3.1 that shows you step-by-step how
I have a multi-step form in which some of the fields are collecting sensitive

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.