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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:24:16+00:00 2026-05-27T03:24:16+00:00

I want to split a string using regular expressions but I have run into

  • 0

I want to split a string using regular expressions but I have run into some problem. I have this string:

$text=" one two three";

Then I try to split it into alphabetic words:

#@words=split(" ", $text);          #1 this works

@words=split("[^a-zA-Z]", $text);   #2 this doesn't work

for $word (@words){
    printf "word: |$word|\n";
}

So the commented method(1) works fine. As expected I get printed:

word: |one|
word: |two|
word: |three|

However with the second method(2) I get this:

word: ||
word: |one|
word: |two|
word: |three|

So although logically the second method should be equivalent to the first one, in practice it doesn’t behave the same way. Why is that?

  • 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-27T03:24:16+00:00Added an answer on May 27, 2026 at 3:24 am

    This is a special case in Perl’s split() function.

    As stated in perldoc:

    split(/PATTERN/, expr, [limit])

    If PATTERN is omitted, [it] splits on whitespace (after skipping any
    leading whitespace).

    Empty leading fields are produced when there are positive-width
    matches at the beginning of the string; […]

    As a special case, specifying a PATTERN of space (' ') will split on
    white space just as split with no arguments does. Thus, split(' ') can
    be used to emulate awk’s default behavior, whereas split(/ /) will
    give you as many initial null fields (empty string) as there are
    leading spaces.

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

Sidebar

Related Questions

I have a String: "{key1:value1}{key2:value2}" . I want to split these string using regular
I have a String, which I want to split into parts using delimeter }},{.
I want to split a string like this: abc//def//ghi into a part before and
I have a string ABCD:10,20,,40;1/1;1/2,1/3,1/4 I want to split the string into the following
Really simple problem: I want to split a connection string into its keyword /
I want to split string without using split . can anybody solve my problem
I want to split a string using a delimiter, for example split "004-034556" into
Using Java (1.6) I want to split an input string that has components of
I want to split a string like 001A into 001 and A
I have an url like this: http://www.w3schools.com/PHP/func_string_str_split.asp I want to split that url to

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.