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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:29:20+00:00 2026-05-22T16:29:20+00:00

I have two strings which contain up to 3 elements: 1) anychar[price]{alphanum} e.g. a1\’)[=00.00]{a1234}

  • 0

I have two strings which contain up to 3 elements:

1) anychar[price]{alphanum} e.g. a1\')[=00.00]{a1234}
2) anychar:anychar{alphanum} e.g. a1\'):a2\'){a1234}

…but the {} element is optional and may not always be there. I wrote the following patterns (respectively):

1) /(.+)\[(.+)\]\{*(\w+)*\}*/ – works as expected

2) /(.+)\:(.+)\{*(\w+)*\}*/ – works fine if the {} element is removed, but not with it.

The result array for 2 is as follows:

(
[0] => a1\'):a2\'){a123}
[1] => a1\')
[2] => a2\'){a123}
)

I’ve tried a few different permutations of the above but no dice. Any ideas?

  • 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-22T16:29:21+00:00Added an answer on May 22, 2026 at 4:29 pm

    First you should remove the * after {, } and (\w+).

    '/(.+)\:(.+)\{(\w+)\}/'
    

    Gives

    array(4) {
      [0]=>
      string(18) "a1\'):a2\'){a1234}"
      [1]=>
      string(5) "a1\')"
      [2]=>
      string(5) "a2\')"
      [3]=>
      string(5) "a1234"
    }
    

    * means either 0, 1 or several, and PCRE tries to find the quickest route it can, so if you make the whole third part optional (by using * everywhere) then the quickest route is to have everything included in the second group and skip the third, that’s why your code didn’t work.

    Now in order to deal with the fact that the third part is optional, you have to use a positive lookahead: in the second group, you will ask pcre to select it only if it can matches another regex after it. The final regex is this:

    '/(.+)\:(.+(?=(?:(?<=[^}])$|\{(\w+)\})))/'
    

    What I changed is that:

    • inside the second group, i added a positive lookahead in the form (?=regex). As said, this means it has to match. Lookahead are not selective by default, which means that they don’t create a entry in your final result/they are not returned to you.

    • inside that lookahead, I created two cases, which means that in order to match, the .+ from the second group will have to match either case of my lookahead.

    • The first case is very basic, it means end of string not preceded by a }, this will match the string when the 3rd part is not there

    • the second case if you selector for the 3rd group, we make it selectable so that it will be returned in the results if present

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

Sidebar

Related Questions

Given two lists, which contain same elements of string but in different orders, is
I have two strings in a java program, which I want to mix in
I have two strings, one a key and one a value, which I would
I have a two-dimensional array (of Strings) which make up my data table (of
I have a function which parses one string into two strings. In C# I
I'm using .NET 3.5. I have two string arrays, which may share one or
I have two types of strings as the IDs of elements in my HTML
I have two web forms which are filled in by the user. They contain
I'm writing a program which is supposed to read two strings that can contain
I have two string variables which are both file paths. The code that worked

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.