0

I need to compare strings such that certain conditions are met. The conditions are not necessarily patterns that I can match using a regex but rather "expression" conditions.

For instance I would like to match a string when a number is between 2 values:

inputString = "The price of a flight to Paris is 550$ and tomorrow will go up by 150$"

matchingCondition = "The price of a flight to Paris is EXPRESSION( 500 < X < 600)$ and tomorrow will go up by EXPRESSION(100 < Y < 200)$"

I need to run this comparison on random texts that don't necessarily look anything like the input string in question.

i.e.

inputString = "blah blah blah"

I would like to be able to compare using a simple true/false function. i.e.:

match(inputString,matchingCondition) -> Boolean

examples:

match(inputString : "The price of the flight is 400$",matchingCondition : "The price of the flight is EXPRESSION( 300$ < X < 401)$") -> true

match(inputString : "The price of the flight is 402$",matchingCondition : "The price of the flight is EXPRESSION( 300$ < X < 401)$") -> false

match(inputString : "Blah blah blah",matchingCondition : "The price of the flight is EXPRESSION( 300$ < X < 401)$") -> false

Is there a way to accomplish this type of comparison (using NSPredicate for example)

Avba
  • 14,822
  • 20
  • 92
  • 192
  • I would consider approaching this, instead of passing a "matching condition", passing a closure, an index range, or something else that allows you to extract the number within the String. Consider one of these functions, depending on your specific use cases: `func match(inputString: String, rangeOfNumberInString: Range, acceptedNumbers: Range) -> Bool` or: `func match(inputString: String, extractNumber:((String)->Int), acceptedNumbers: Range) -> Bool` – Connor Neville Jul 19 '16 at 13:44
  • I need a way to perform the comparison using a DSL since the matching condition is generated dynamically. The way you suggest would work great if I had know exactly what I was looking for before the comparison is necessary. – Avba Jul 19 '16 at 13:50
  • If you look for price (or address too in fact in you check the distance, etc.), I think that NSDataDetector may help you to find the range for Connor Neville idea. – Larme Jul 20 '16 at 14:39
  • NSDataDetector will return pre-defined match types. I'm looking for a general way to provide expressions in a matching pattern – Avba Jul 25 '16 at 07:24

0 Answers0