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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:54:37+00:00 2026-06-15T00:54:37+00:00

I want to understand how can I do arithmetic on matched sub-patterns in perl

  • 0

I want to understand how can I do arithmetic on matched sub-patterns in perl regex.
This is just a sample code and I want to understand how can I use \1 (already matched sub-pattern. in this case – 7) to match pattern+1 (8)

my $y = 77668;
if($y =~ /(\d)\1(\d)\2\1+1/)   #How to increment a previously
                               #matched sub-pattern and form a pattern?
{
    print $y;
}

EDIT

From the answers, I see that pattern arithmetic is not possible.

This is what I want to achieve.
I want to form a regex which will match this pattern:

N-3N-2N-1NNN+1N+2N+3    (N = 3,4,5,6
  • 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-15T00:54:39+00:00Added an answer on June 15, 2026 at 12:54 am

    Its possible via regex code blocks:

    my $y = 77668;
    if($y =~ /(\d)\1(\d)\2(??{$1+1})/ ) {
        print $y;
    }
    

    In this snippet (??{ CODE }) returns another regex that must match, so this regex looks like “8” ($1+1). As a result, whole regex will match only if 5th digit is greather and 1st by 1. But drawback with 1st digit is 9, this code block will return “10”, so possible its wrong behavior, but you said nothing about what must be done in this case.

    Now about N-3N-2N-1NNN+1N+2N+3 question, you can match it with this regex:

    my $n = 5;
    if( $y =~ /(??{ ($n-3).($n-2).($n-1).$n.($n+1).($n+2).($n+3) })/ ){
    

    Or more “scalable” way:

    my $n = 5;
    if( $y =~ /(??{ $s=''; $s .= $n+$_ foreach(-3..3); $s; })/ ){
    

    Again, what we must do if $n == 2 ?? $n-3 will be -1. Its not a simply digit cus it have sign, so you should think about this cases.

    One another way. Match what we have and then check it.

    if( $y =~ /(\d)(\d)(\d)(\d)(\d)(\d)(\d)/ ) {
        if( $1 == ($4-3) && $2 == ($4-2) && $3 == ($4-1) && $6 == ($4+1) && $7 == ($4+2) && $7 == ($4+3) ) {
            #...
    

    Seems this method litle bit clumsy, but its obivious to everyone (i hope).

    Also, you can optimize your regex since 7 ascending digits streak is not so frequent combination, plus get some lulz from co-workers xD:

    sub check_number {
        my $i;
        for($i=1; $i<length($^N); $i++) {
            last if substr($^N, $i, 1)<=substr($^N, $i-1, 1);
        }
        return $i<length($^N) ? "(*FAIL)" : "(*ACCEPT)";
    }
    
    if( $y =~ /[0123][1234][2345][3456][4567][5678][6789](??{ check_number() })/ ) {
    

    Or… Maybe most human-friendly method:

    if( $y =~ /0123456|1234567|2345678|3456789/ ) {
    

    Seems last variant is bingo xD Its good example about not searching regex when things are so simple)

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

Sidebar

Related Questions

I just want to understand how capturing and bubbling work. Unfortunately this code just
Hi I can't understand this error I want read this XML file: <?xml version=1.0
I want to understand the use of xargs man in Rampion's code : screen
I want ask about preg_match how i can understand /\s*;\s*/ In code preg_split('/\s*;\s*/', $something);
I want to understand how I can update my GUI with a simple text
I have a .exe app which I want to understand better - I can
I want to better understand the capabilities and limitations of what can be done
I understand that I can call ToString().IndexOf(...), but I don't want to create an
I want to understand the actual theory behind types rather than just learning about
I want to understand if code snippets are what I am looking for here.

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.