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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:11:47+00:00 2026-05-11T12:11:47+00:00

This was originally a question I wanted to ask, but while researching the details

  • 0

This was originally a question I wanted to ask, but while researching the details for the question I found the solution and thought it may be of interest to others.

In Apache, the full request is in double quotes and any quotes inside are always escaped with a backslash:

1.2.3.4 - - [15/Apr/2005:20:35:37 +0200] 'GET /\' foo=bat\' HTTP/1.0' 400 299 '-' '-' '-' 

I’m trying to construct a regex which matches all distinct fields. My current solution always stops on the first quote after the GET/POST (actually I only need all the values including the size transferred):

^(\d+\.\d+\.\d+\.\d+)\s+[^\s]+\s+[^\s]+\s+\[(\d+)/([A-Za-z]+)/(\d+):(\d+):(\d+):(\d+)\s+\+\d+\]\s+'[^']+'\s+(\d+)\s+(\d+|-) 

I guess I’ll also provide my solution from my PHP source with comments and better formatting:

$sPattern = ';^' .     # ip address: 1     '(\d+\.\d+\.\d+\.\d+)' .     # ident and user id     '\s+[^\s]+\s+[^\s]+\s+' .     # 2 day/3 month/4 year:5 hh:6 mm:7 ss +timezone     '\[(\d+)/([A-Za-z]+)/(\d+):(\d+):(\d+):(\d+)\s+\+\d+\]' .     # whitespace     '\s+' .     # request uri     ''[^']+'' .     # whitespace     '\s+' .     # 8 status code     '(\d+)' .     # whitespace     '\s+' .     # 9 bytes sent     '(\d+|-)' .     # end of regex     ';'; 

Using this with a simple case where the URL doesn’t contain other quotes works fine:

1.2.3.4 - - [15/Apr/2005:20:35:37 +0200] 'GET /\ foo=bat\ HTTP/1.0' 400 299 '-' '-' '-' 

Now I’m trying to get support for none, one or more occurrences of \' into it, but can’t find a solution. Using regexpal.com I’ve came up with this so far:

^(\d+\.\d+\.\d+\.\d+)\s+[^\s]+\s+[^\s]+\s+\[(\d+)/([A-Za-z]+)/(\d+):(\d+):(\d+):(\d+)\s+\+\d+\]\s+'(.|\\(?='))*' 

Here’s only the changed part:

    # request uri     ''(.|\\(?='))*'' . 

However, it’s too greedy. It eats everything until the last ', when it should only eat until the first ' not preceded by a \. I also tried introducing the requirement that there’s no \ before the ' I want, but it still eats to the end of the string (Note: I had to add extraneous \ characters to make this work in PHP):

    # request uri     ''(.|\\(?='))*[^\\\\]'' . 

But then it hit me: *?: If used immediately after any of the quantifiers , +, ?, or {}, makes the quantifier non-greedy (matching the minimum number of times)

    # request uri     ''(.|\\(?='))*?[^\\\\]'' . 

The full regex:

^(\d+\.\d+\.\d+\.\d+)\s+[^\s]+\s+[^\s]+\s+\[(\d+)/([A-Za-z]+)/(\d+):(\d+):(\d+):(\d+)\s+\+\d+\]\s+'(.|\\(?='))*?[^\\]'\s+(\d+)\s+(\d+|-) 

Update 5th May 2009:

I discovered a small flaw in the regexp due parsing millions of lines: it breaks on lines which contain the backslash character right before the double quote. In other words:

...\\' 

will break the regex. Apache will not log ...\' but will always escape the backslash to \\, so it’s safe to assume that when there’re two backslash characters before the double quote.

Anyone has an idea how to fix this with the the regex?

Helpful resources: the JavaScript Regexp documentation at developer.mozilla.org and regexpal.com

  • 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. 2026-05-11T12:11:47+00:00Added an answer on May 11, 2026 at 12:11 pm

    Try this:

    '(?:[^\\']+|\\.)*' 

    This regular expression matches a double quote character followed by a sequence of either any character other than \ and ' or an escaped sequence \α (where α can be any character) followed by the final double quote character. The (?:expr) syntax is just a non-capturing group.

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Subject lines are the only universal way I'm afraid. Have… May 11, 2026 at 1:37 pm
  • added an answer Even though it is best practice to call IDisposable.Dispose() yourself… May 11, 2026 at 1:37 pm
  • added an answer To add a context menu to your addin, you create… May 11, 2026 at 1:37 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.