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

The Archive Base Latest Questions

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

I need to split a list between its first item and the rest of

  • 0

I need to split a list between its first item and the rest of its items using regex substitution only.

The lists of items are input as strings using ‘##’ as a separator, e.g.:

''
'one'
'one##two'
'one##two##three'
'one##two words##three'

My Perl attempt doesn’t really work:

my $sampleText = 'one##two words##three';
my $first = $sampleText;
my $rest = $sampleText;
$first =~ s/(.+?)(##.*)?/$1/g;
$rest =~ s/(.?+)(##)?(.*)/$3/g;
print "sampleText = '$sampleText', first = '$first', rest = '$rest'\n";

sampleText = 'one##two words##three', first = 'one', rest = 'ne##two words##three'

Please note the constraints:

  • the separator is a multi-character string
  • only regex substitutions are allowed (1)
  • I could “chain” regex substitutions if necessary
  • The expected end result is two strings: the first element, and the initial string with the first element cut off (2)
  • the list may have from 0 to n items, each being any string not containing the separator.

(1) I work with this rather large Perl system where at some point lists of items are processed using provided operations. One of them is a regex substitution. None of the others one are applicable. Solving the problem using full Perl code is easy, but that would mean modifying the system, which is not an option as this time.

(2) the context is the Unimarc bibliographic format, where authors of a publication are to be split into the standard Unimarc fields 700$a for the first author, and 701$a for any remaining authors.

  • 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-27T07:52:17+00:00Added an answer on May 27, 2026 at 7:52 am

    I assume point (1) means you cannot use the split builtin? It would be easy using splits optional third parameter which lets you specify the maximum number of items.

    my( $first, $rest ) = split( '##', $sampleText, 2 );
    

    But if it has to be regex replace then your is almost right, but using .+? wont work when there’s no sperators (because it will just take the first character You can fix this by anchoring the end. Instead something like:

    my $sampleText = 'one##two words##three';
    my $first = $sampleText;
    my $rest = $sampleText;
    $first =~ s/(.+?)(|##(.*))$/$1/g;
    $rest  =~ s/(.+?)(|##(.*))$/$3/g;
    print "sampleText = '$sampleText', first = '$first', rest = '$rest'\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to split a list into two equal lists. For Example: I have
I need to split my string input into an array at the commas. Is
need to split every 8 char so it will become a list whereby i
I have a list of numbers and I need to split into to corresponding
I need to split a string into a list of parts in Ruby, but
I have a List that I need to split into sublists, one for each
I want to split List of user generic List into its small list with
I'm seeking an algorithm to split a list of items of varying sizes into
I need split string by commas and spaces, but ignore the inside quotes, single
I need to split an flv file into chunks of the known size on

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.