0

语言不可知论者。

提供任何语言的解决方案,首选常用脚本语言。

处理字符串...我们将在数组中使用这 4 个作为示例。

examples = ["The quick brown fox jumps over the lazy dog.(JSON-CAT5).tar.gz",
            "The quick brown fox jumps over the lazy dog.(JSON-CAT5).txt",
            "The quick & swift mule, kicks the lazy dev.txt",
            "Now we-come,to.the_payoff.txt"]

根据以下规则对其进行转换。

  1. 无论任何其他规则如何,首字母总是大写。
  2. 每个单词都大写,除非它被规则 3 或 4 覆盖。
  3. 出现在白名单中的子字符串会保留大小写。在我们的例子中["the", "JSON"]
  4. 从字符串中清除出现在黑名单中的子字符串。在我们的例子中["-CAT5","(",")"]
  5. 匹配正则表达式的子字符串/(\.tar)?\.[^.]*$/i始终为小写。
  6. 标点符号列表例如[" ", "_", ",", "-"]被转换为 ["."]
  7. 多个"."即。"..."替换为单个"."(即挤压。)
  8. 分隔符“.”、黑名单、白名单都应该可以轻松互换,只需将它们作为 vars / arrays 提供在顶部是完全可以接受的。

在这种情况下,我们最终会得到:

  1. The.Quick.Brown.Fox.Jumps.Over.the.Lazy.Dog.JSON.txt
  2. The.Quick.Brown.Fox.Jumps.Over.the.Lazy.Dog.JSON.tar.gz

下面提供了答案,希望看到其他语言的替代方案。

更新

向测试用例添加了更多示例字符串。

4

4 回答 4

1

红宝石示例

#!/usr/bin/env ruby

examples = ["The quick brown fox jumps over the lazy dog.(JSON-CAT5).tar.gz",
            "The quick brown fox jumps over the lazy dog.(JSON-CAT5).txt",
            "The quick & swift mule, kicks the lazy dev.txt",
            "Now we-come,to.the_payoff.txt"]

result = []

whitelist = ["we", "to", "the", "of", "if", "a", "is", "JSON", "HTTP", "HTTPS", "HTML"] # you get the idea.
blacklist = ["-CAT5", "(", ")"]
separators = ["-", "_", ",", " "]
separator = "."

examples.each do |eg|

  extension = eg.match(/(\.tar)?\.[^.]*$/i)[0]
  eg.sub!(extension, "")
  blacklist.each{ |b| eg.gsub!(b, "") }
  separators.each{ |s| eg.gsub!(s, separator) }
  words = eg.split(".")
  words.each {|w|
    w.capitalize! if words.first == w
    unless whitelist.include? w
      w.capitalize!
    end
  }
  n = "#{words.join('.')}#{extension}".squeeze(".")
  result << n
end

puts result

给我们...

The.Quick.Brown.Fox.Jumps.Over.the.Lazy.Dog.JSON.tar.gz
The.Quick.Brown.Fox.Jumps.Over.the.Lazy.Dog.JSON.txt
The.Quick.&.Swift.Mule.Kicks.the.Lazy.Dev.txt
Now.we.Come.to.the.Payoff.txt

可能的缺点,白名单和黑名单区分大小写。

于 2013-01-02T14:01:40.207 回答
1
ArrayList final = new ArrayList();

String[] arr = input.split("[\\W]");

final.add(arr[0].charAt(0).toUpper() + arr[0].substring(1));

for(int i=1; i<length; i++)
{
    if(whitelist.contains(arr[i]))
    {
        final.add(arr[i]);
        continue;
    }

    if(blacklist.contains(arr[i]))
    {
        continue;
    }

    arr[i] = arr[i].charAt(0).toUpper() + arr[i].substring(1);

}

String output = "";

int i=0;
for(i=0; i<finalList.size()-1;i++)
    output += finalList.get(i) + ".";

output += finalList.get(i);
于 2013-01-02T14:16:18.653 回答
1

我是初学者 Perl 程序员,在这里我尝试使用 Perl 完成您的任务:

Perl 示例:

#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

my @examples = (
  qq(The quick brown fox jumps over the lazy dog.(JSON-CAT5).tar.gz),
  qq(The quick brown fox jumps over the lazy dog.(JSON-CAT5).txt),
  qq(The quick & swift mule, kicks the lazy dev.txt),
  qq(Now we-come,to.the_payoff.txt)
);

my @result;

my @whitelist = ( 'we', 'to', 'the', 'of', 'if', 'a', 'is', 'JSON' );
my @blacklist = ( '-CAT5', '(',')' );
my @separators = ( '-', '_', ' ', ',' );
my $separator = '.';

foreach my $eg (@examples) {
  $eg =~ s/(\.tar)?\.[^.]*$//pi;

  my $ext = ${^MATCH};

  my $blist = join('|', map { qr/\Q$_/i } @blacklist );
  my $slist = join('|', map { qr/\Q$_/i } @separators );

  $eg =~ s/$blist//gi;
  $eg =~ s/$slist/$separator/gi;

  my @vals = split /\./, $eg;
  push my @words, ucfirst shift @vals;
  foreach my $w (@vals) {
    $w = ucfirst $w unless $w ~~ @whitelist;
    push @words, $w;
  }

  $eg = join $separator, @words;
  $eg .= $ext;
  $eg =~ s/\.\.+/\./g; # squeeze
  push @result, $eg;
}

say for @result;
于 2013-01-02T20:22:07.790 回答
1

没有什么比玩 python 更好的事情了……

Python 示例

import re

examples = (
  "The quick brown fox jumps over the lazy dog.(JSON-CAT5).tar.gz",
  "The quick brown fox jumps over the lazy dog.(JSON-CAT5).txt",
  "The quick & swift mule, kicks the lazy dev.txt",
  "Now we-come,to.the_payoff.txt"
)

result = []

whitelist = ( 'we', 'to', 'the', 'of', 'if', 'a', 'is', 'JSON' )
blacklist = ( '-CAT5', '(', ')' )
separators = ( '-', '_', ' ', ',' )
separator = '.'

for eg in examples:
    ext = re.search(r'(\.tar)?\.[^.]*$', eg).group(0)
    eg = eg.replace(ext, '')

    for ignore in blacklist:
        eg = eg.replace(ignore, '')

    for sep in separators:
        eg = eg.replace(sep, separator)

    vals = enumerate(eg.split('.'))

    words = [w.capitalize() if i == 0 or not w in whitelist else w for i,w in vals]
    words.append(ext)

    eg = separator.join(words)
    eg = re.sub(r'\.\.+', '.', eg) # squeeze

    result.append(eg)

for r in result:
    print r
于 2013-02-06T13:51:23.760 回答