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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:07:28+00:00 2026-05-27T06:07:28+00:00

This appears to be strange behavior, or perhaps I don’t understand regular expressions so

  • 0

This appears to be strange behavior, or perhaps I don’t understand regular expressions so well…

I’m using this to find all the xref and trailer objects in a PDF file:

preg_match_all('@(\nxref\r?\n)|(\strailer\s)@',$pdfcontent,$matches,PREG_OFFSET_CAPTURE);

print_r gives me this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] =>
xref
                    [1] => 13235519
                )

            [1] => Array
                (
                    [0] =>
trailer
                    [1] => 13299371
                )
        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] =>
xref
                    [1] => 13235519
                )

            [1] => Array
                (
                    [0] =>
                    [1] => -1
                )
        )

    [2] => Array
        (
            [0] =>
            [1] => Array
                (
                    [0] =>
trailer
                    [1] => 13299371
                )
        )
)

Why is there a position of -1 for xref?

  • 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-27T06:07:29+00:00Added an answer on May 27, 2026 at 6:07 am

    It seems this is the normal behaviour, mostly undocumented though. The -1 offset is also used for absent matches.

    To answer your title, the -1 offset is returned alternatively, not in addition. You have an alternative (a)|(b) match group in your pattern. So it can very well return offsets and matches for the xref, but a non-match for the trailer.

    This is not mentioned explicitely in the PHP manual page. But PCRE documents it cursorily with:

    […] When this happens, both values in the offset pairs corre-
    sponding to unused subpatterns are set to -1.

    You can reproduce it with a simpler example:

    preg_match_all('/(a)|(b)|(c)/', "abc", $m, PREG_OFFSET_CAPTURE)
    and print_r($m);
    

    [Have a look]. The behaviour is a bit confusing. It seems the -1 is used as offset for the early non-matches. But subsequent failed matches are just absent in the result array. This example gives [0,-1,-1] and [undef,1,-1] and [undef,undef,2] for example. I would conclude it’s some hazy behaviour in the PHP wrapper.

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

Sidebar

Related Questions

This is a really strange issue. When using WebClient.DownloadFile in Debug configuration, it appears
This is a really strange problem, that appears to be somewhat intermittent (although it
This function appears to be a way to access all sorts of system values.
Using the BlockUI JQuery plugin I find strange behaviour from within an ASP.Net page.
Winforms' ListBox appears to have a strange behavior. When I set the SelectionMode to
My iphone application is showing strange behavior when rotating: a gap appears between the
I am experiencing strange behavior using git stash, I have two branches v0 and
I have strange MPMoviePlayerController behavior. It don't want to play movies just chosen from
From what I've already read this appears to be impossible, but I wanted to
Possible Duplicate: Plugging in to Java compilers Edit - this appears to be a

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.