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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:05:31+00:00 2026-05-23T21:05:31+00:00

I am working with the Amazon Mechanical Turk API and it will only allow

  • 0

I am working with the Amazon Mechanical Turk API and it will only allow me to use regular expressions to filter a field of data.

I would like to input an integer range to a function, such as 256-311 or 45-1233, and return a regex that would match only that range.

A regex matching 256-321 would be:

\b((25[6-9])|(2[6-9][0-9])|(3[0-1][0-9])|(32[0-1]))\b

That part is fairly easy, but I am having trouble with the loop to create this regex.

I am trying to build a function defined like this:

function getRangeRegex( int fromInt, int toInt)
{

      return regexString;
}

I looked all over the web and I am surprised that it doesn’t look like anyone has solved this in the past. It is a difficult problem…

Thanks for your time.

  • 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-23T21:05:33+00:00Added an answer on May 23, 2026 at 9:05 pm

    Here’s a quick hack:

    <?php
    
    function regex_range($from, $to) {
    
      if($from < 0 || $to < 0) {
        throw new Exception("Negative values not supported"); 
      }
    
      if($from > $to) {
        throw new Exception("Invalid range $from..$to, from > to"); 
      }
    
      $ranges = array($from);
      $increment = 1;
      $next = $from;
      $higher = true;
    
      while(true) {
    
        $next += $increment;
    
        if($next + $increment > $to) {
          if($next <= $to) {
            $ranges[] = $next;
          }
          $increment /= 10;
          $higher = false;
        }
        else if($next % ($increment*10) === 0) {
          $ranges[] = $next;
          $increment = $higher ? $increment*10 : $increment/10;
        }
    
        if(!$higher && $increment < 10) {
          break;
        }
      }
    
      $ranges[] = $to + 1;
    
      $regex = '/^(?:';
    
      for($i = 0; $i < sizeof($ranges) - 1; $i++) {
        $str_from = (string)($ranges[$i]);
        $str_to = (string)($ranges[$i + 1] - 1);
    
        for($j = 0; $j < strlen($str_from); $j++) {
          if($str_from[$j] == $str_to[$j]) {
            $regex .= $str_from[$j];
          }
          else {
            $regex .= "[" . $str_from[$j] . "-" . $str_to[$j] . "]";
          }
        }
        $regex .= "|";
      }
    
      return substr($regex, 0, strlen($regex)-1) . ')$/';
    }
    
    function test($from, $to) {
      try {
        printf("%-10s %s\n", $from . '-' . $to, regex_range($from, $to));
      } catch (Exception $e) {
        echo $e->getMessage() . "\n";
      }
    }
    
    test(2, 8);
    test(5, 35);
    test(5, 100);
    test(12, 1234);
    test(123, 123);
    test(256, 321);
    test(256, 257);
    test(180, 195);
    test(2,1);
    test(-2,4);
    
    ?>
    

    which produces:

    2-8        /^(?:[2-7]|8)$/
    5-35       /^(?:[5-9]|[1-2][0-9]|3[0-5])$/
    5-100      /^(?:[5-9]|[1-9][0-9]|100)$/
    12-1234    /^(?:1[2-9]|[2-9][0-9]|[1-9][0-9][0-9]|1[0-2][0-3][0-4])$/
    123-123    /^(?:123)$/
    256-321    /^(?:25[6-9]|2[6-9][0-9]|3[0-2][0-1])$/
    256-257    /^(?:256|257)$/
    180-195    /^(?:18[0-9]|19[0-5])$/
    Invalid range 2..1, from > to
    Negative values not supported
    

    Not properly tested, use at your own risk!

    And yes, the generated regex could be written more compact in many cases, but I leave that as an exercise for the reader 🙂

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

Sidebar

Related Questions

I am working for a scientific project and would like to use the Amazon
I'm working with Amazon's Product Advertising API and need some help on a few
working PHP probably on amazon ec2... can anyone explain what would be the best
I'm working on an application that utilizes Amazon's MWS API written mostly in PHP.
I am working on an interface with the amazon product advertising API. I have
I am trying to build a working encrypted signature for the Amazon S3 web
I'm working on a Hadoop streaming workflow for Amazon Elastic Map Reduce and it
I'm trying to get Heroku working with European buckets on amazon s3 webserivces using
I took my first step today on working with cloud servers and chosed Amazon
I'm using Amazon S3 to put the mp3 file then allow our site visitor

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.