English 中文(简体)
页: 1 只有一个标题(简单!)
原标题:Ruby Regex, Only One Capture (Very Simple!)
  • 时间:2011-10-03 17:58:56
  •  标签:
  • ruby
  • regex

我猜测,这将是一丝不 the的错误,但对于我来说,以下几个阵列只包含“M”。 参见:

/(.)+?/.match("Many many characters!").captures
=> ["M"]

为什么它回到了各种特征? 我必须 missed开一些明显的事实,因为我看不到什么错误?

Edit: Just realised, I 无需+? 但它仍然没有工作。

Edit: 道歉! 我将澄清:我的目标是使用户能够进入定期表达和打字以及输入文本档案,只要有匹配,文本将四舍五入,并且将适用捆绑,我不仅将插手分割成特性,而且我只使用特定的舱面,因为它是最简单的,尽管我对此感到困惑。 我怎样从扫描中抓获团体,还是不可能这样做? 我看到,1美元包含“

Edit: Gosh, 确实是我的一天。 正如杰克告诉我的那样,这些捕获物储存在单独的阵列中。 我如何从原来的扼杀中得到这些捕获的抵消? 我希望能够从随后又一次的抓获中获。 或者说,什么是sub? (我认为这只是取而代之,而不是集团)

www.un.org/Depts/DGACM/index_spanish.htm 希望最后编辑: 权利,让我再次开始:

因此,我有一点。 用户将使用配置文件输入定期的表述,然后使用与每个捕获组相关的风格。 我需要能够扫描整个护堤,并找到每个团体的起步和完成或缩小规模。

因此,如果用户配置了<代码>([w-.]+)@(?:[w]+.)+([a-zA-Z]{2,4})(电子邮箱地址),我就应当能够:

[ ["elliotpotts", 0,  11],
  ["sample.",     12, 7],
  ["com",         19, 3] ]

from the string: "elliotpotts@sample.com"

如果这不明确,我就根本不喜欢:P。 感谢迄今为止的许多gu,并感谢你如此耐心!

最佳回答

因为你的捕获只是对one的单一特性。 <代码>(>+与(+)不相同。

>> /(.)+?/.match("Many many characters!").captures
=> ["M"]
>> /(.+)?/.match("Many many characters!").captures
=> ["Many many characters!"]
>> /(.+?)/.match("Many many characters!").captures
=> ["M"]

如果您想对每一特性进行重新使用,String#scanString#split 如果你不关心俘虏团体

使用扫描:

"Many many characters!".scan(/./)
#=> ["M", "a", "n", "y", " ", "m", "a", "n", "y", " ", "c", "h", "a", "r", "a", "c", "t", "e", "r", "s", "!"]

请注意,其他答案是使用(<>>>>>,而如果你关注抓获集团,则罚款数额很小,否则,如果你不收 t,就会把EVERY CHARACTER送回自己单独的Array,例如:

[["M"], ["a"], ["n"], ["y"], [" "], ["m"], ["a"], ["n"], ["y"], [" "], ["c"], ["h"], ["a"], ["r"], ["a"], ["c"], ["t"], ["e"], ["r"], ["s"], ["!"]]

否则,仅使用<代码>split:-Many many natures>,split( )

EDIT In reply to your edit:

reg = /([w-.]+)@((?:[w]+.)+)([a-zA-Z]{2,4})/
str = "elliotpotts@sample.com"
str.scan(reg).flatten.map { |capture| [capture, str.index(capture), capture.size] }
#=> [["elliotpotts", 0, 11], ["sample.", 12, 7], ["com", 19, 3]]`

Oh, and you don t need scan, you re not really scanning so you dont need to traverse, at least not with the example you provided:

str.match(reg).captures.map { |capture| [capture, str.index(capture), capture.size] }

Will also work

问题回答

是的,重要的问题被忽略;

<代码>(......) 只引入“一>捕获组:由于该指数是由正常表述本身而不是输入确定的 > 仅限<> /em” ,因此该组对应数不相干。

关键是“全球定期表达”,它将多次采用定期表达方式。 在Ruby,这样做是因为从Regex#matchString#s(其他许多语文有“/g”经常表达改动):

"Many many chara­cters!".sc­an(/(.)+?/­)
# but more simply (or see answers using String#split)
"Many many chara­cters!".sc­an(/(.)/­)

1. 幸福 co

它只是恢复一种特性,因为你们都要求它这样做。 或许希望使用<代码>scan:

str = "Many many characters!"
matches = str.scan(/(.)/)




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签