简体   繁体   English

红宝石中的正则表达式多个步骤

[英]RegEx multiple steps in ruby

Given the string below:鉴于以下字符串:

"oxcCFC/video.mp4 "GET /accounts/6/videos/xboxcCFC/video.mp4 HTTP/1.1" 206 - 2 697898511 56 56 "-""

How would I create a regular expression that first finds "HTTP, then finds "-", and then captures the next indiviual number or consecutive numbers that occurs in the sequence?我将如何创建一个正则表达式,首先找到“HTTP”,然后找到“-”,然后捕获序列中出现的下一个单个数字或连续数字?

I'm trying to use rubular but struggling big time.我正在尝试使用 rubular 但很费时间。

Not a lot to go on, but I think it should be something like this:没什么可做的,但我认为它应该是这样的:

/^.*\s+HTTP.*\s+-\s+(\d+)\s+/

A backreference will then hold the value you're after.然后反向引用将保存您所追求的值。

我想看看你是否可以使用 apachelogregex gem ... http://rubydoc.info/gems/apachelogregex/0.1.0/frames

Short answer: /HTTP[^-]*-([\\d\\s]+)/ then, call split on the result.简短回答: /HTTP[^-]*-([\\d\\s]+)/然后,对结果调用split

This regexp translates to:此正则表达式转换为:

"HTTP", followed by any number of non-hyphen characters, followed by a hyphen, followed by the largest string consisting only of digits and spaces. “HTTP”,后跟任意数量的非连字符字符,后跟连字符,然后是仅由数字和空格组成的最大字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM