使用Ruby脚本

时间:2016-06-06 13:20:18

标签: ruby

我得到了一个名称如下的照片列表:

/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59-1.jpg
/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59.jpg
/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54-1.jpg
/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54.jpg
[...]

我正在尝试删除名称“相似”的所有照片。我想要做的是某种模式匹配。

如何判断两个字符串的前n个字符是否相同?

3 个答案:

答案 0 :(得分:2)

ErrorMessage

答案 1 :(得分:2)

  

如何判断前两个字符是否相同   字符串?

根据上下文,你真正想要的是“找出两个字符串开头有多少个字符相同?”如果是这样的话:

s1 = "hello there!"
s2 = "helloween"
s1.chars.zip(s2.chars).take_while { |c1, c2| c1 == c2 }.count
#=> 5

答案 2 :(得分:2)

问题

在行之间阅读,似乎你已经确定了

arr = [
  '/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59-1.jpg',
  '/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59.jpg',
  '/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59-2.jpg',
  '/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.55-21.jpg',
  '/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54-2.jpg',
  '/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54.jpg',
  '/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.55-32.jpg',
  '/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.56-1.jpg'
]

文件arr[0,3]是相同的,因为文件名的不同只是可选的包含连字符,后跟一个或多个数字,在".jpg"之前立即,并且您要删除所有但是组中有一个文件(即删除三个文件中的两个)。此外,如果组中的一个文件不包含可选连字符后跟'.jpg"之前的一个或多个数字,则该文件 - “基本文件” - 是不被删除的文件。同样,要删除组[arr[3], arr[6]]和组arr[4,2]中除一个文件以外的所有文件。组[arr[7]]仅包含一个文件,因此不会删除该组中的任何文件。

<强>代码

您可以使用正则表达式

来实现
r = /
    \A           # match beginning of string
    .+?          # match one or more of any character, lazily
    (?=          # begin a positive lookahead
      (?:-\d+)?  # optionally (?) match hypen, one or more digits, in a non-capture group 
      \.jpg\z    # match ".jpg" followed by end of string
    )            # end positive lookahead
    /x           # free-spacing regex definition mode

与实例方法Enumerable#group_byString#[]Hash#valuesEnumerable#flat_mapArray#-以及类方法File::#delete结合使用:

arr.group_by { |f| f[r] }.values.flat_map { |a| a-[a.max] }.each { |f| File.delete(f) } 

<强>解释

以下步骤。

h = arr.group_by { |f| f[r] }
  #=> {"/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59"=>
  #      ["/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59-1.jpg",
  #       "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59.jpg",
  #       "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59-2.jpg"],
  #    "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.55"=>
  #      ["/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.55-21.jpg",
  #    "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.55-32.jpg"],
  #    "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54"=>
  #      ["/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54-2.jpg",
  #       "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54.jpg"],
  #    "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.56"=>
  #      ["/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.56-1.jpg"]} 
v = h.values
  #=> [["/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59-1.jpg",
  #     "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59.jpg",
  #     "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59-2.jpg"],
  #    ["/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54-2.jpg",
  #     "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54.jpg"],
  #    ["/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.55-21.jpg",
  #     "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.55-32.jpg"],
  #    ["/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.56-1.jpg"]]
b = v.flat_map { |a| a-[a.max] }
  #=> ["/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59-1.jpg",
  #    "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.05.59-2.jpg",
  #    "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.55-21.jpg",
  #    "/Users/foo/Desktop/argentinien-chile 2/2009-12-21 17.16.54-2.jpg"] 

请注意,因为"." > "-" #=> true,如果一个组包含“基本文件”,则它将是a.max;因此,未删除的文件。

b.each { |f| File.delete(f) }