是否有方法/ gem用一些基本搜索参数创建正则表达式。
e.g。
ProtobufDatumWriter<MyProto> pbWriter = new ProtobufDatumWriter<MyProto>(MyProto.class);
DataFileWriter<MyProto> dataFileWriter = new DataFileWriter<MyProto>(pbWriter);
Schema schema= ProtobufData.get().getSchema(MyProto.class);
dataFileWriter.create(schema, new File("test.avro"));
dataFileWriter.append(myProto);
dataFileWriter.close();
这样正则表达式将搜索(不区分大小写):
&#34;德国牧羊犬&#34; - 确切地说 要么 &#34;科利&#34; 要么 &#34;贵宾犬&#34; 要么 &#34;微型&#34; AND&#34;雪纳瑞&#34;
所以在这种情况下就像:
Search = ["\"German Shepherd\"","Collie","poodle", "Miniature Schnauzer"]
(对更好的方法做最后一点的建议......)
答案 0 :(得分:2)
如果我理解了这个问题,请转到:
regexps = ["\"German Shepherd\"","Collie","poodle", "Miniature Schnauzer"]
# those in quotes
greedy = regexps.select { |re| re =~ /\A['"].*['"]\z/ } # c'"mon, parser
# the rest unquoted
non_greedy = (regexps - greedy).map(&:split).flatten
# concatenating... ⇓⇓⇓ get rid of quotes
all = Regexp.union(non_greedy + greedy.map { |re| re[1...-1] })
#⇒ /Collie|poodle|Miniature|Schnauzer|German\ Shepherd/
<强> UPD 强>
我终于得到了Miniature Schnauzer
要做的事情(请参阅下面的评论以获得进一步的解释。)这就是说,这些词语将被置换并加入非贪婪.*?
:
non_greedy = (regexps - greedy).map(&:split).map do |re|
# single word? YES : NO, permute and join
re.length < 2 ? re : re.permutation.map { |p| Regexp.new p.join('.*?') }
end.flatten
all = Regexp.union(non_greedy + greedy.map { |re| re[1...-1] })
#=> /Collie|poodle|(?-mix:Miniature.*?Schnauzer)|(?-mix:Schnauzer.*?Miniature)|German\ Shepherd/