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

  • Home
  • SEARCH
  • 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 64749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:50:19+00:00 2026-05-10T18:50:19+00:00

Suppose I have the following two strings containing regular expressions. How do I coalesce

  • 0

Suppose I have the following two strings containing regular expressions. How do I coalesce them? More specifically, I want to have the two expressions as alternatives.

$a = '# /[a-z] #i'; $b = '/ Moo /x'; $c = preg_magic_coalesce('|', $a, $b); // Desired result should be equivalent to: // '/ \/[a-zA-Z] |Moo/' 

Of course, doing this as string operations isn’t practical because it would involve parsing the expressions, constructing syntax trees, coalescing the trees and then outputting another regular expression equivalent to the tree. I’m completely happy without this last step. Unfortunately, PHP doesn’t have a RegExp class (or does it?).

Is there any way to achieve this? Incidentally, does any other language offer a way? Isn’t this a pretty normal scenario? Guess not. 🙁

Alternatively, is there a way to check efficiently if either of the two expressions matches, and which one matches earlier (and if they match at the same position, which match is longer)? This is what I’m doing at the moment. Unfortunately, I do this on long strings, very often, for more than two patterns. The result is slow (and yes, this is definitely the bottleneck).

EDIT:

I should have been more specific – sorry. $a and $b are variables, their content is outside of my control! Otherwise, I would just coalesce them manually. Therefore, I can’t make any assumptions about the delimiters or regex modifiers used. Notice, for example, that my first expression uses the i modifier (ignore casing) while the second uses x (extended syntax). Therefore, I can’t just concatenate the two because the second expression does not ignore casing and the first doesn’t use the extended syntax (and any whitespace therein is significant!

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T18:50:20+00:00Added an answer on May 10, 2026 at 6:50 pm

    I see that porneL actually described a bunch of this, but this handles most of the problem. It cancels modifiers set in previous sub-expressions (which the other answer missed) and sets modifiers as specified in each sub-expression. It also handles non-slash delimiters (I could not find a specification of what characters are allowed here so I used ., you may want to narrow further).

    One weakness is it doesn’t handle back-references within expressions. My biggest concern with that is the limitations of back-references themselves. I’ll leave that as an exercise to the reader/questioner.

    // Pass as many expressions as you'd like function preg_magic_coalesce() {     $active_modifiers = array();      $expression = '/(?:';     $sub_expressions = array();     foreach(func_get_args() as $arg) {         // Determine modifiers from sub-expression         if(preg_match('/^(.)(.*)\1([eimsuxADJSUX]+)$/', $arg, $matches)) {             $modifiers = preg_split('//', $matches[3]);             if($modifiers[0] == '') {                 array_shift($modifiers);             }             if($modifiers[(count($modifiers) - 1)] == '') {                 array_pop($modifiers);             }              $cancel_modifiers = $active_modifiers;             foreach($cancel_modifiers as $key => $modifier) {                 if(in_array($modifier, $modifiers)) {                     unset($cancel_modifiers[$key]);                 }             }             $active_modifiers = $modifiers;         } elseif(preg_match('/(.)(.*)\1$/', $arg)) {             $cancel_modifiers = $active_modifiers;             $active_modifiers = array();         }          // If expression has modifiers, include them in sub-expression         $sub_modifier = '(?';         $sub_modifier .= implode('', $active_modifiers);          // Cancel modifiers from preceding sub-expression         if(count($cancel_modifiers) > 0) {             $sub_modifier .= '-' . implode('-', $cancel_modifiers);         }          $sub_modifier .= ')';          $sub_expression = preg_replace('/^(.)(.*)\1[eimsuxADJSUX]*$/', $sub_modifier . '$2', $arg);          // Properly escape slashes         $sub_expression = preg_replace('/(?<!\\\)\//', '\\\/', $sub_expression);          $sub_expressions[] = $sub_expression;     }      // Join expressions     $expression .= implode('|', $sub_expressions);      $expression .= ')/';     return $expression; } 

    Edit: I’ve rewritten this (because I’m OCD) and ended up with:

    function preg_magic_coalesce($expressions = array(), $global_modifier = '') {     if(!preg_match('/^((?:-?[eimsuxADJSUX])+)$/', $global_modifier)) {         $global_modifier = '';     }      $expression = '/(?:';     $sub_expressions = array();     foreach($expressions as $sub_expression) {         $active_modifiers = array();         // Determine modifiers from sub-expression         if(preg_match('/^(.)(.*)\1((?:-?[eimsuxADJSUX])+)$/', $sub_expression, $matches)) {             $active_modifiers = preg_split('/(-?[eimsuxADJSUX])/',                 $matches[3], -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);         }          // If expression has modifiers, include them in sub-expression         if(count($active_modifiers) > 0) {             $replacement = '(?';             $replacement .= implode('', $active_modifiers);             $replacement .= ':$2)';         } else {             $replacement = '$2';         }          $sub_expression = preg_replace('/^(.)(.*)\1(?:(?:-?[eimsuxADJSUX])*)$/',             $replacement, $sub_expression);          // Properly escape slashes if another delimiter was used         $sub_expression = preg_replace('/(?<!\\\)\//', '\\\/', $sub_expression);          $sub_expressions[] = $sub_expression;     }      // Join expressions     $expression .= implode('|', $sub_expressions);      $expression .= ')/' . $global_modifier;     return $expression; } 

    It now uses (?modifiers:sub-expression) rather than (?modifiers)sub-expression|(?cancel-modifiers)sub-expression but I’ve noticed that both have some weird modifier side-effects. For instance, in both cases if a sub-expression has a /u modifier, it will fail to match (but if you pass 'u' as the second argument of the new function, that will match just fine).

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

Sidebar

Related Questions

Suppose I have strings like the following : OneTwo ThreeFour AnotherString DVDPlayer CDPlayer I
This is a scjp mock exam question. Suppose I have the following two files:
Suppose I have following string: String asd = this is test ass this is
Suppose I have the following CSS rule in my page: body { font-family: Calibri,
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
Suppose you have the following string: white sand, tall waves, warm sun It's easy
Suppose I have the following declaration: class Over1 { protected: class Under1 { };
Suppose I have the following C code. unsigned int u = 1234; int i
Suppose I have the following directory layout in a Maven project: src/ |-- main
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require

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.