Tag: 模式匹配

SQLite3“LIKE”或PostgreSQL“ILIKE”的通用Ruby解决方案?

我使用SQLite3进行开发,使用PostgreSQL进行部署。 但是,我面临以下问题: 我使用SQLite3简单搜索: def self.search(search) if search find(:all, :conditions => [“style LIKE ? OR construction LIKE ?”, “%#{search}%”, “%#{search}%”]) else find(:all) end end 但是,它不适用于PostgreSQL ,我需要替换ILIKE的LIKE才能解决问题: def self.search(search) if search find(:all, :conditions => [“style ILIKE ? OR construction ILIKE ?”, “%#{search}%”, “%#{search}%”]) else find(:all) end end 是否有“Ruby方式”在任何数据库中进行这些搜索? 编辑 – 基于您的答案我不相信我会找到一个通用的Ruby解决方案。 我遵循了Ruby on Rails教程:通过示例学习Rails – 作者:Michael Hartl […]

如何检测字符串内相同的部分?

我试图将解码算法想要的问题分解成更小的问题。 这是第一部分。 题: 两个字符串:s1和s2 s1的一部分与s2的一部分相同 空间是分隔符 如何提取相同的部分? 例1: s1 = “12 November 2010 – 1 visitor” s2 = “6 July 2010 – 100 visitors” the identical parts are “2010”, “-“, “1” and “visitor” 例2: s1 = “Welcome, John!” s2 = “Welcome, Peter!” the identical parts are “Welcome,” and “!” 例3 :(澄清“!”示例) s1 = “Welcome, Sam!” […]

如何获取字符串中所有模式的索引

string = “Jack and Jill went up the hill to fetch a pail of water. Jack fell down and broke his crown. And Jill came tumbling after. ” d = string.match(/(jack|jill)/i) # -> MatchData “Jill” 1:”Jill” d.size # -> 1 这只匹配它看起来的第一次出现。 string.scan部分完成了这项工作,但它没有说明匹配模式的索引。 如何获得模式的所有匹配实例及其索引(位置)的列表?