使用正则表达式

时间:2015-11-12 08:02:19

标签: ruby regex

Ruby回溯跟踪消息:

["/home/ec2-user/LogglyRails/RailsApp/app/controllers/pages_controller.rb:19:in `sampleRailsNullPointerException'", 
"/home/ec2-user/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_controller/metal/implicit_render.rb:4:in `send_action'"]

注意:ruby back trace消息没有换行符。

预期比赛:

/home/ec2-user/LogglyRails/RailsApp/app/controllers/pages_controller.rb
19
sampleRailsNullPointerException
/home/ec2-user/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_controller/metal/implicit_render.rb
4
send_action

我试图设计正则表达式:

^(?P<path>[^:]*):(?P<line>\d+):in\s+`(?P<exception>\w+)$

但这个正则表达式与要求不符。

3 个答案:

答案 0 :(得分:0)

re = /^(?<path>[^:]*):(?<line>\d+):in\s+`(?<exception>\w+)'$/

trace = [
  "/home/ec2-user/LogglyRails/RailsApp/app/controllers/pages_controller.rb:19:in `sampleRailsNullPointerException'", 
  "/home/ec2-user/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_controller/metal/implicit_render.rb:4:in `send_action'"
]

trace.map { |line| re.match(line).captures }
# => [["/home/ec2-user/LogglyRails/RailsApp/app/controllers/pages_controller.rb", "19", "sampleRailsNullPointerException"], ["/home/ec2-user/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_controller/metal/implicit_render.rb", "4", "send_action"]]

你在行结束前忘记了结束语;另外,我不知道你对(?P...)的意思。

答案 1 :(得分:0)

试试这个:

s = "/home/ec2-user/LogglyRails/RailsApp/app/controllers/pages_controller.rb:19:in `sampleRailsNullPointerException'"
m = s.match(/^([^:]+?):(\d+):in\s+`(\w+)'$/)
m[1] #=> "/home/ec2-user/LogglyRails/RailsApp/app/controllers/pages_controller.rb"
m[2] #=> "19"
m[3] #=> "sampleRailsNullPointerException"

答案 2 :(得分:0)

如果 <PropertyGroup> <Property1>Example</Property1> </PropertyGroup> <Target Name="$(Property1)"> 是你的数组,你可以写:

arr