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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:45:26+00:00 2026-06-03T20:45:26+00:00

I have a string like this: $var = ‘123456d’; or possibly $var = ‘123456’;

  • 0

I have a string like this:

$var = '123456d';

or possibly

$var = '123456'; # (no alpha char)

There will be up to 6 digits no less than 5 if that matters.
No alpha, spaces or special mixed in the numbers.

In my Perl script, I need to remove and return the last alpha character AND the string without the alpha character.

I suppose can use \D and trim? or chop? but, there must be an easy way to get both vars quickly.

my $numbers =~   s/\D+?$var//;  #???  

my $alpha = substr($var, 0, -1); ## but no alpha check.

or

my $alpha = chop($var); ## but no alpha check.

then if a-Z and so on to check if it is an alpha character.

Or the limit of my ability solution:

$alpha = $var;
$alpha =~ s/[0-9]//ig;
$var =~ s/[a-Z]//ig;
$numbers = $var;

so result:

($numbers == '123456')
($alpha eq 'd') (or '' if nothing)

I feel like this is too trivial to ask here but, I just cannot find or write an applicable solution.

My use of =~ s is just a guess but, there must be a better way or even a one liner.

Thanks for all the help here…

SORRY after thought! There may be 2 alpha characters after the digits! They would need to be returned together as a single var ($alpha eq ‘dd’)

I found this too to0:

$var =~ s{^([0-9]+).*}{$1}i;
  • 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-03T20:45:27+00:00Added an answer on June 3, 2026 at 8:45 pm

    Using \pL will avoid capturing numbers, and making the string optional will assure that the match happens even for numbers without ending letters.

    my $var = '123456d';
    my ($num, $letter) = $var =~ /^(\d+)(\pL)?$/;
    

    Note that you cannot use \w to capture the possible ending letter, because it also includes numbers. As tchrist has pointed out, \p{alpha} can also be used.

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

Sidebar

Related Questions

I have a string like this: var string = ' [United States] [Canada] [India]
I have a string contaning some html markup, like this: var html = <div>
I have an ordered list like this: var list = new List<String>(){aaa,aab,aac,baa,bab,bac}; I want
i have a string like this var url=http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11 i want to get the string
I have a string like this: var str = I'm a very^ we!rd* Str!ng.;
i have a string like this . var url=http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11 i want to get substrings
I have string like this var str = #it itA itB _ #et etA
I have a string that looks like this: var expression = @Args(token1) + Args(token2);
I have a string created using asp.net MVC3 Razor block like this: @{ var
I have a string something like this var test = 'Hello you have multiple

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.