如何在ruby中执行以下正则表达式?

时间:2009-10-17 22:34:12

标签: ruby regex

我如何将以下各项与正则表达式匹配?

some text includes [any number of character].需要拉出整个 [asdfasdf]

@tableid='sometext'我需要提取 sometext

mary and drugs我需要拉“和”,包括空格。

2 个答案:

答案 0 :(得分:2)

irb(main):002:0> "some text include [abcdef]".match(/\[(.*)\]/)[1]
=> "abcdef"

irb(main):005:0> "@table_id='2356'".match(/'(.*)'/)[1]
=> "2356"

irb(main):006:0> "mary and drugs".match(/mary(.*)drugs/)[1]
=> " and "

答案 1 :(得分:0)

试试这些:

"\[(.*?)\]"
"@\w+='(.*?)'"
" +and +"