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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:22:09+00:00 2026-05-27T08:22:09+00:00

I am able to split strings in the format key:value; using the following code:

  • 0

I am able to split strings in the format key:value; using the following code:

$inside = "key1:value1;key2:value2;key3:value3;";
preg_match_all("/([^:]+):([^;]+);/s", $inside, $pairs);

What I would like to do is allow for the occurrence of the colon and semi-colon character in the values by introducing an escape character e.g. \; any colon or semi-colon immediately preceded by a backslash would be ignored.

Bonus points if within the same regex, the escaped characters can then be stored in the array of matches unescaped without having to run everything through str_replace. Thanks for any help you can offer.

  • 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-27T08:22:10+00:00Added an answer on May 27, 2026 at 8:22 am
    preg_match_all(
        '/(                    # Match and capture...
         (?:                   # either:
          \\\\.                # an escaped character
         |                     # or:
          [^\\\\:]             # any character except : or \
         )+                    # one or more times
        )                      # End of capturing group 1
        :                      # Match a colon
        ((?:\\\\.|[^\\\\;])+); # Same for 2nd part with semicolons
        /x', 
        $inside, $pairs);
    

    does this. It doesn’t remove the backslashes, though. You can’t do that in a regex itself; for this, you’d need a callback function.

    To match the final element even if it doesn’t end with a delimiter change the ; to (?:;|$) (same for the :). And to return empty elements as well change the + to a *.

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

Sidebar

Related Questions

I want to be able to split csv strings in Oracle 9i I've read
I need to split a string into chunks of 2,2,3,3 characters and was able
I want to be able to split a big test to smaller tests so
I got a file which has dates stored as the following format (yyyy|dMM|dd). I
I'm looking to split space-delimited strings into a series of search terms. However, in
I need to be able to split an input String by commas, semi-colons or
I have an app where a property file contains data in the following format
I need to split up some strings, but the number of characters and position
I've got some code that I don't think is able to be multithreaded, perhaps
I have code to read in the version number from a make file. VERSION_ID=map(int,re.match(VERSION_ID\s*=\s*(\S+),open(version.mk).read()).group(1).split(.))

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.