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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:34:29+00:00 2026-05-13T22:34:29+00:00

The Perl FAQ entry How do I strip blank space from the beginning/end of

  • 0

The Perl FAQ entry How do I strip blank space from the beginning/end of a string? states that using

s/^\s+|\s+$//g;

is slower than doing it in two steps:

s/^\s+//;
s/\s+$//;

Why is this combined statement noticeably slower than the separate ones (for any input string)?

  • 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-13T22:34:29+00:00Added an answer on May 13, 2026 at 10:34 pm

    The Perl regex runtime runs much quicker when working with ‘fixed’ or ‘anchored’ substrings rather than ‘floated’ substrings. A substring is fixed when you can lock it to a certain place in the source string. Both ‘^’ and ‘$’ provide that anchoring. However, when you use alternation ‘|’, the compiler doesn’t recognize the choices as fixed, so it uses less optimized code to scan the whole string. And at the end of the process, looking for fixed strings twice is much, much faster than looking for a floating string once. On a related note, reading perl’s regcomp.c will make you go blind.

    Update:
    Here’s some additional details. You can run perl with the ‘-Dr’ flag if you’ve compiled it with debugging support and it’ll dump out regex compilation data. Here’s what you get:

    ~# debugperl -Dr -e 's/^\s+//g'
    Compiling REx `^\s+'
    size 4 Got 36 bytes for offset annotations.
    first at 2
    synthetic stclass "ANYOF[\11\12\14\15 {unicode_all}]".
       1: BOL(2)
       2: PLUS(4)
       3:   SPACE(0)
       4: END(0)
    stclass "ANYOF[\11\12\14\15 {unicode_all}]" anchored(BOL) minlen 1
    

    # debugperl -Dr -e 's/^\s+|\s+$//g'
    Compiling REx `^\s+|\s+$'
    size 9 Got 76 bytes for offset annotations.
    
       1: BRANCH(5)
       2:   BOL(3)
       3:   PLUS(9)
       4:     SPACE(0)
       5: BRANCH(9)
       6:   PLUS(8)
       7:     SPACE(0)
       8:   EOL(9)
       9: END(0)
    minlen 1 
    

    Note the word ‘anchored’ in the first dump.

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

Sidebar

Related Questions

Perl: How can I sort a complex structure using JSON::PP ? From the JSON
Perl has the \u operator to lowercase a match when using string replacement and
In Perl, what is a good way to perform a replacement on a string
In Perl, you can execute system commands using system() or `` (backticks). You can
I am using Perl's DBD::ODBC to connect to an Oracle database. However, an issue
I am just begining to learn Perl. I looked at the beginning perl page
I'm trying to use a Perl module from CPAN ( AuthCookieDBI.pm to be specific)
I am working on a small perl program that will open a site and
I am having trouble understanding / using name spaces with XML::LibXML package in Perl.
Perl allows you to use the '..' operator to return a slice from an

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.