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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:24:14+00:00 2026-05-24T20:24:14+00:00

I have a bunch of strings like this: my $string1 = xg0000; my $string2

  • 0

I have a bunch of strings like this:

my $string1 = "xg0000";
my $string2 = "fx0015";

What do I do to increase the number in the string by 1 but also maintain the leading zeros to keep the length of the string the same.

I tried this:

$string =~ s/(\d+)/0 x length(int($1)) . ($1+1)/e;

It doesn’t seem to work on all numbers. Is regex what I’m supposet to use to do this or is there a better way?

  • 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-24T20:24:14+00:00Added an answer on May 24, 2026 at 8:24 pm

    How about a little perl magic? The ++ operator will work even on strings, and 0000 will magically turn into 0001.

    Now, we can’t modify $1 since it is readonly, but we can use an intermediate variable.

    use strict;
    use warnings;
    my $string = "xg0000"; 
    $string =~ s/(\d+)/my $x=$1; ++$x/e; 
    

    Update:

    I didn’t think of this before, but it actually works without a regex:

    C:\perl>perl -we "$str = 'xg0000'; print ++$str;"
    xg0001
    

    Still does not solve the problem DavidO pointed out, with 9999. You would have to decide what to do with those numbers. Perl has a rather interesting solution for it:

    C:\perl>perl -we "$str = 'xg9999'; print ++$str;"
    xh0000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of strings in perl that all look like this: 10
So, I have a bunch of strings like this: {\b\cf12 よろてそ } . I'm
I have a file with a bunch of strings that looks like this: new
I have List collection with around 35,000 strings Typical string looks like this: <i>füüs</i>ampri
I have a bunch of strings in php that all look like this: 10
I have a bunch of strings that look, for example, like this: <option value=Spain>Spain</option>
I have a JList that holds a bunch of strings . I would like
I have a bunch of strings I need to use .Trim() on, but they
I'm writing a bunch of strings to a file using a string writer but
I have a bunch of strings extracted from html using jQuery. They look like

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.