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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:16:17+00:00 2026-05-15T00:16:17+00:00

I have a string where different predefined keywords introduce different data. Is there a

  • 0

I have a string where different predefined keywords introduce different data. Is there a way to do that using clever use of regexp, or something? Here is an example:

Keywords can be "first name: " and "last name: ". Now I want to parse:

"character first name: Han last name: Solo"

into

{ "first name: " => "Han ", "last name: " => "Solo" }

Of course, the order of the keywords in the input string is not fixed. This should also work on :

"character last name: Solo first name: Han"

I understand there are issues to be raised with spaces and so on. I’ll ignore them here.

I know how to solve this problem looping on the different keywords, but I don’t find that very pretty.

Split almost fits the bill. Its only problem is that it returns an array and not a hash, so I don’t know which is the first name or the last name.

My example is somewhat misleading. Here is another one:

my @keywords = ("marker 1", "marker 2", "marker 3");
my $rawString = "beginning marker 1 one un marker 2 two deux marker 3 three trois and the rest";
my %result;
# <grind result>
print Dumper(\%result);

will print:

$VAR1 = {
      'marker 2' => ' two deux ',
      'marker 3' => ' three trois and the rest',
      'marker 1' => ' one un '
    };
  • 1 1 Answer
  • 2 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-15T00:16:18+00:00Added an answer on May 15, 2026 at 12:16 am

    Here is a solution using split (with separator retention mode) that is extensible with other keys:

    use warnings;
    use strict;
    
    my $str = "character first name: Han last name: Solo";
    
    my @keys = ('first name:', 'last name:');
    
    my $regex = join '|' => @keys;
    
    my ($prefix, %hash) = split /($regex)\s*/ => $str;
    
    print "$_ $hash{$_}\n" for keys %hash;
    

    which prints:

    last name: Solo
    first name: Han 
    

    To handle keys that contain regex metacharacters, replace the my $regex = ... line with:

     my $regex = join '|' => map {quotemeta} @keys;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have 2 string arrays with different values: string[] sArray1 =
I have a string array of 3 different command line commands. Instead of writing
I have a string pc1|pc2|pc3| I want to get each word on different line
I have around 2 million strings with different lengths that I need to compress
I have a website that has approx 1000 different strings in a mysql database
Is it possible to have same hashcode for different strings using java's hashcode function?or
I want to use two different Spring web contexts , each have own contextConfig,
I wrote a serializer (to Byte Array) for dictionaries that have a string key,
I have a webpage that is a Search page. There are a few pre-defined
if I have a string in the format of (static string) name (different static

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.