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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:27:54+00:00 2026-06-12T07:27:54+00:00

I need to separate the key and values from the text that looks like

  • 0

I need to separate the key and values from the text that looks like below

Student ID:  0
Department ID =          18432
Name                        XYZ

Subjects:
Computer Architecture
Advanced Network Security 2

In the above example Student ID, Department ID and Name are the keys and 0,18432, XYZ are values. The keys are separated from the values either by :,= or multiple spaces. I tried reg ex such as

    $line =~ /(([\w\(\)]*\s)*)([=:\s?]?)\s*(\S.*)?$/;
    $key   = $2;
    $colon=$3;
    $value = $4;

The problem I am facing is identifying when a word is separated with single space and when it is separated by more than one.

The output I get is
line is Student ID: 0
key is Student , value is ID: 0
while I want key is Student ID and value is 0. For lines like Subjects: and Computer Architecture, the key should have Subjects and Computer Architecture. I have logic later when there is no value or colon, I append the strings to the previous key so it will look like Subjects=Computer Architecture;Advanced Network Security 2

Update: Thanks Ikegami for indicating that I use look behind operator. But I still seem to have problem solving it.

$line=~/^(?: ( [^:=]+ ) (?<!\s\s)\s* [:=]\s*|\s*)(.*)$/x;

So When I say (?<!\s\s)\s* [:=]\s*|\s* I mean when there more than two spaces, consume all the spaces and when there are no two consecutive spaces look for : or = and consume spaces. So if you pass below line to the expression, Shouldnt I be getting $1=Name and $2=ABC XYZ?

Name         ABC XYZ

What I seem to be getting is key is empty and value is Name ABC XYZ.

  • 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-12T07:27:55+00:00Added an answer on June 12, 2026 at 7:27 am

    If

    Name Eric Brine
    Computer Architecture x86
    

    means

    key: Name Eric               value: Brine
    key: Computer Architecture   value: x86
    

    then you want

    # Requires 5.10
    if (/
       ^
       (?: (?<key> [^:=]+ (?<!\s) ) \s* [:=] \s* (?<val> .*  )
       |   (?<key> .+     (?<!\s) ) \s+          (?<val> \S+ )
       )
       \s* $
    /x) {
       my $key = $+{key};
       my $val = $+{val};
       ...
    }
    

    or

    if (/
       ^
       (?: ( [^:=]+ (?<!\s) ) \s* [:=] \s* ( .*  )
       |   ( .+     (?<!\s) ) \s+          ( \S+ )
       )
       \s*
       ( .* )
    /x) {
       my ($key,$val) = defined($1) ? ($1,$2) : ($3,$4);
       ...
    }
    

    If

    Name Eric Brine
    Computer Architecture x86
    

    means

    key: Name       value: Eric Brine
    key: Computer   value: Architecture x86
    

    then you want

    # Requires 5.10
    if (/
       ^
       (?: (?<key> [^:=]+ (?<!\s) ) \s* [:=]
       |   (?<key> \S+ ) \s
       )
       \s*
       (?<val> .* )
    /x) {
       my $key = $+{key};
       my $val = $+{val};
       ...
    }
    

    or

    if (/
       ^
       (?: ( [^:=]+ (?<!\s) ) \s* [:=]
       |   ( \S+ ) \s
       )
       \s*
       ( .* )
    /x) {
       my $key = defined($1) ? $1 : $2;
       my $val = $3;
       ...
    }
    

    Note that you can remove all the space and line breaks. For example, the last snippet can be written as:

    if (/^(?:([^:=]+(?<!\s))\s*[:=]|(\S+)\s)\s*(.*)/) {
       my $key = defined($1) ? $1 : $2;
       my $val = $3;
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need to accept a collection of key values from user input. Then a
I am retrieving number of values from database, that need to be added to
What I'm trying to do is get 3 values from a key into separate
I need to separate out a bunch of image urls from a document in
I need to separate all possible suffixes (about 1000) from a given word. I
In the Dot Net Nuke module that I'm developing I need to separate the
I need to retrieve information from two separate models which are similar but not
I need to take rows from two separate tables, and arrange them in descending
Here is an example bit from the xml file: <array> <dict> <key>Name</key> <string>Joe Smith</string>
I have some inputs that I need to process from a form. The #

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.