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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:18:38+00:00 2026-05-20T20:18:38+00:00

I am trying to figure out how to capture one statement if the other

  • 0

I am trying to figure out how to capture one statement if the other one doesn’t exist using preg_match.

Sample Text:

<!-- InstanceBeginEditable name="doctitle" -->

<title>BU Libraries | Research Guides | Citing Your Sources</title>

<!-- InstanceEndEditable -->

<div id="standardpgt"><h1><!-- InstanceBeginEditable name="pagetitle" --><strong>Citing Your Sources</strong><!-- InstanceEndEditable --></h1></div>

Because pagetitle exists I want to pull it instead of the doctitle tag. Of course there is tons of other characters in between them, but I wanted to show you a small sample.

If pagetitle didn’t exist I would want to grab the contents of doctitle.

The twist is that I’m not using the php code directly, I’m passing in a regex statement through a config file, then a script is taking it and pulling out the 1st group from the statement.

This is what I came up with:

((?!.*?<!--\s*?InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->.*?<!--\s*?InstanceEndEditable\s*?-->)<!--\s*?InstanceBeginEditable\s*?name=\x22doctitle\x22\s*?-->\s*?<title>(.*?)<\/title>\s*?<!--\s*?InstanceEndEditable\s*?-->|<!-- InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->(.*?)<!--\s*?InstanceEndEditable\s*?-->)

What the issue is for some reason php always reads the first empty group as group 1 if it didn’t work.

For example in the sample text above it would return

0 -> <!-- InstanceBeginEditable name="pagetitle" --><strong>Citing Your Sources</strong><!-- InstanceEndEditable -->
1 -> 
2 -> <strong>Citing Your Sources</strong>

I can’t for the life of figure out how to make this work. I also wrote this regex:

(?(?=.*?<!--\s*?InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->.*?<!--\s*?InstanceEndEditable\s*?-->).*?<!-- InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->(.*?)<!--\s*?InstanceEndEditable\s*?-->|.*?<!--\s*?InstanceBeginEditable\s*?name=\x22doctitle\x22\s*?-->\s*?<title>(.*?)<\/title>\s*?<!--\s*?InstanceEndEditable\s*?-->)

But that didn’t work either. Thank you very much for the help.

Chris

  • 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-20T20:18:38+00:00Added an answer on May 20, 2026 at 8:18 pm

    user178551 is absolutely correct in recommending the use of a branch reset construct. There is fundamentally nothing wrong with your original regex (other than the fact that it is more than 300 characters long and is ALL ON ONE LINE! – and that it is unable to put one of two alternatives in a single capture group). A non-trivial (to put it mildly) regex like this needs to be written in free-spacing mode with indentation so you can actually read it. Here is your original regex with some reasonable whitespace added:

    $re_OP1 = '%
        (                                             # $1:
          (?!
            .*?<!--\s*?InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->
            .*?<!--\s*?InstanceEndEditable\s*?-->
          )
               <!--\s*?InstanceBeginEditable\s*?name=\x22doctitle\x22\s*?-->\s*?
               <title>(.*?)<\/title>\s*?              # $2: 
               <!--\s*?InstanceEndEditable\s*?-->
        |      <!-- InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->
               (.*?)                                  # $3;
               <!--\s*?InstanceEndEditable\s*?-->
        )
        %six';
    

    Looking at this regex now, you can see where you have hard coded one space on the line with the OR operator (i.e. |<!-- InstanceBegin...). This will cause the regex to fail to match with the 'x' modifier is applied. So replacing this space with a \s* and running it on your test data, here are the result I get (php-5.2.14):

    Array
    (
        [0] => <!-- InstanceBeginEditable name="pagetitle" --><strong>Citing Your Sources</strong><!-- InstanceEndEditable -->
        [1] => <!-- InstanceBeginEditable name="pagetitle" --><strong>Citing Your Sources</strong><!-- InstanceEndEditable -->
        [2] =>
        [3] => <strong>Citing Your Sources</strong>
    )
    

    These results are similar to the ones you posted (but for some reason your results show only 2 capture groups???) All we need to do now is to apply user178551’s branch reset suggestion, and the regex solution becomes:

    $re_jmr = '%
        (?|  # Branch reset construct. (restart counting for each alternative)
          (?!
            .*?<!--\s*InstanceBeginEditable\s*name="pagetitle"\s*-->
            .*?<!--\s*InstanceEndEditable\s*-->
          )
               <!--\s*InstanceBeginEditable\s*name="doctitle"\s*-->\s*
               <title>(.*?)<\/title>\s*              # $1: Group 1A
               <!--\s*InstanceEndEditable\s*-->
        |      <!--\s*InstanceBeginEditable\s*name="pagetitle"\s*-->
               (.*?)                                  # $1: Group 1B
               <!--\s*InstanceEndEditable\s*-->
        )
        %six';
    

    I’ve gone ahead and changed all the lazy \s*? to greedy (because greedy is what you want here). I also changed all the \x22 to just " – shorter and more readable IMHO. And here are the results from running with this new, branch reset regex:

    Array
    (
        [0] => <!-- InstanceBeginEditable name="pagetitle" --><strong>Citing Your Sources</strong><!-- InstanceEndEditable -->
        [1] => <strong>Citing Your Sources</strong>
    )
    

    Which is, (if I’m not mistaken), exactly what you are looking for. (You did not provide a test case for the other alternative so that has not yet been tested.) Other than that, your original regex was pretty close.

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

Sidebar

Related Questions

I am trying to figure out if there is a way to capture using
I'm trying to figure out how to capture data from a form using EditText
trying to figure out why this is happening - I have an input text
Been trying to figure out, how do I capture the events from a listbox.
I'm trying to figure out how to capture an error message for the following
I'm trying to figure out how to capture a fully rendered page and manipulate
I'm trying to figure out how exactly to use stat() to capture information about
Trying to figure out how I can do this properly. The print_r looks like
Trying to figure out why my silverlight app suddenly just displays nothing (right click
Trying to figure out how to type (via events not set the value) on

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.