Project

textquery

0.05
No commit activity in last 3 years
No release in over 3 years
Evaluate any text against a collection of match rules
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

 Project Readme

TextQuery

Does it match? When regular expressions are not enough, textquery is the answer. For example, regular expressions cannot evaluate recursive rules and often result in overly verbose and complicated expressions.

Textquery is a simple PEG grammar with support for:

  • AND (spaces are implicit AND's)
  • OR
  • NOT (- is an alias)
  • 'quoted strings'
  • fuzzy matching
  • case (in)sensitive
  • attribute tags (e.g. surname:Smith)
  • custom delimiters (default is whitespace for words, : for attributes)

TextQuery in the wild: PostRank, PaperTrail, and others!

Example

TextQuery.new("'to be' OR NOT 'to_be'").match?("to be")   # => true

TextQuery.new("-test").match?("some string of text")      # => true
TextQuery.new("NOT test").match?("some string of text")   # => true

TextQuery.new("a AND b").match?("b a")                    # => true
TextQuery.new("a AND b").match?("a c")                    # => false

q = TextQuery.new("a AND (b AND NOT (c OR d))")
q.match?("d a b")                                         # => false
q.match?("b")                                             # => false
q.match?("a b cdefg")                                     # => true

TextQuery.new("a~").match?("adf")                         # => true
TextQuery.new("~a").match?("dfa")                         # => true
TextQuery.new("~a~").match?("daf")                        # => true
TextQuery.new("2~a~1").match?("edaf")                     # => true
TextQuery.new("2~a~2").match?("edaf")                     # => false

TextQuery.new("a", :ignorecase => true).match?("A b cD")  # => true

License

The MIT License - Copyright (c) 2011 Ilya Grigorik