Ruby:找到被.sub替换的内容

时间:2015-08-24 17:26:11

标签: ruby regex

给出代码

thisString.sub! /piece\_[0-9]{1,4}.ts/, "piece_#{i}.ts"

其中thisString可以是从piece_1.tspiece_9999.ts

的任何内容

如何获取被替换的号码?

1 个答案:

答案 0 :(得分:0)

作为权宜之计,我最终获得了这个数字,然后做了子:

foundStartPiece = Integer(firstline[/piece\_[0-9]{1,4}.ts/][/[0-9]{1,4}/]) # Returns the number as an integer

编辑:之后,我确实转向使用@ndn的回答