我愿建议一种不同于旧的解决办法。 注:旧代码使用deprecated>。 顺便说一句,它向铁路(<>/strong>)倾斜,你在问题(仅作为tag)中明确提到铁路。 此外,现有的解决办法未能按照你的要求将<代码>.doc.pdf并入_doc.pdf
。 当然,这并没有使强调崩溃。
我的解决办法是:
def sanitize_filename(filename)
# Split the name when finding a period which is preceded by some
# character, and is followed by some character other than a period,
# if there is no following period that is followed by something
# other than a period (yeah, confusing, I know)
fn = filename.split /(?<=.).(?=[^.])(?!.*.[^.])/m
# We now have one or two parts (depending on whether we could find
# a suitable period). For each of these parts, replace any unwanted
# sequence of characters with an underscore
fn.map! { |s| s.gsub /[^a-z0-9-]+/i, _ }
# Finally, join the parts with a period and return the result
return fn.join .
end
您没有具体说明有关转换的所有细节。 因此,我提出以下假设:
- There should be at most one filename extension, which means that there should be at most one period in the filename
- Trailing periods do not mark the start of an extension
- Leading periods do not mark the start of an extension
- Any sequence of characters beyond
A
–Z
, a
–z
, 0
–9
and -
should be collapsed into a single _
(i.e. underscore is itself regarded as a disallowed character, and the string $%__°#
would become _
– rather than ___
from the parts $%
, __
and °#
)
其复杂部分是,我把档案名称分为主要部分和延伸部分。 在定期表达的帮助下,我搜索了最后一段时期,之后是一段时期之外的东西,因此,在一段时期里没有达到同样的标准。 然而,在进行之前,必须具有某种特性,以确保它不至于扼杀的第一个特性。
我测试了这一职能:
1.9.3p125 :006 > sanitize_filename my§document$is°° very&interesting___thisIs%nice445.doc.pdf
=> "my_document_is_very_interesting_thisIs_nice445_doc.pdf"
我认为,这是你的要求。 我希望这够了。