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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:39:47+00:00 2026-06-07T10:39:47+00:00

So, I have a regexp that I have tested on Rubular and from the

  • 0

So, I have a regexp that I have tested on Rubular and from the CLI (using the pry gem). This parses a custom Apache log format. When I feed input to it in pry, it works as expected (e.g. $~ is populated.) Rubular also reports correct matching and grouping for various lines of input. When run from the code below, no matches.

I have also tried messing with String.chomp! and the \n character, in case that was throwing off the match, but various permutations have no effect.

I’m sure it’s something a more experienced Rubyist could shed some light on.

Rubular link: http://www.rubular.com/r/fycHVYZdZz

Here is the relevant code, regex, and input — and thanks in advance:

log_regex = %r{
            (?<ip>(([0-9]{1,3}\.){3}[0-9]{1,3}))                                                                   
            \s-\s
            (?<src_ip>.*)
            -\s
            (?<date>\[.*\])
            \s
            (?<url>".+")
            \s
            (?<response>\d{3})
            \s
            (?<length>\d+)
            \s
            (?<referer>".+")
            \s
            (?<useragent>".*")
            \s(?<host>.*)?
            /ix
            }

logfile = ARGV[0]

def process_log(log_regex,logfile)
    IO.foreach(logfile, 'r') do |line|
        line.chomp!
        log_regex.match(line) do |m|
            puts m['ip']
        end
    end
end

process_log(log_regex,logfile)

Sample input:

209.123.123.123 - - [05/Jul/2012:11:02:01 -0700] "GET /url/mma/rss2.0.xml HTTP/1.1" 301 0 "-" "FeedBurner/1.0 (http://www.FeedBurner.com)" xml.somewhere.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. Editorial Team
    Editorial Team
    2026-06-07T10:39:49+00:00Added an answer on June 7, 2026 at 10:39 am

    You probably want to take a close look at the definition of your regex. Your flags are inside the definition of the pattern, instead of following the closing of %r where they belong:

    %r{
    ...
    /ix
    }
    

    should be:

    %r{
    ...
    }ix
    

    From IRB:

    irb(main):001:0> %r{foo/ix}
    /foo\/ix/
    irb(main):002:0> %r{foo}ix
    /foo/ix
    irb(main):003:0> %r{^foo$}ix =~ 'foo'
    0
    irb(main):004:0> %r{^foo/ix$} =~ 'foo'
    nil
    

    Both PRY and IRB return the same results for the above tests.

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

Sidebar

Related Questions

I have got this regex pattern that works with my current apache log format:
I have the following regexp that I am using to validate an email; @Pattern(regexp=[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?,
I have this working regex (tested on regex coach): \n[\s]*[0-9]*[\s]*[0-9]*(\.)?[0-9]*(e\+)?[0-9]* that is supposed to
i have this regex string that i got from a website to pull emails
I have this regex that checks for a 5 digit number ^\d{5}$ How do
I have this regex that detects hashtags. It shouldn't match things with letters before
I have this regex that uses forward and backward look-aheads: import re re.compile(<!inc\((?=.*?\)!>)|(?<=<!inc\(.*?)\)!>) I'm
I have this regex that is supposed to match 1='aa' or 1=aa or 1=aa,
Let's say I have two strings like this: XABY XBAY A simple regex that
I have an .htaccess file that's using several rewrite rules. These rules aren't working

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.