-1

I would like to match a sentence if few words are present and discard the match if certain other words are present.

For example:

I would like to regex match to happen if the string has "consumer" and "services" and the match not to happen if the string has word "delayed".

Match to happen:

"Consumer are offered services based on the plans selected"

Match not to happen:

"In case ,the consumer services are delayed then penalty shall be beared."

Regex written for match:

(\b(?:consumer)\b.*?services)
DonRaHulk
  • 575
  • 1
  • 6
  • 18

2 Answers2

1

Do you really need a regular expression for this? I think the code will be much easier to understand without one:

input = 'Consumer are offered services based on the plans selected'

input_lower = input.downcase

input_lower.include?('consumer') &&
  input_lower.include?('services') &&
  !input_lower.include?('delayed')

This isn't exactly checking what you asked since it's not looking for word boundaries (e.g. 'Consumers' will also match), but that's probably desirable anyway.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
0

You might use the negative lookahead:

▶ input = ["Consumer are offered services based on the plans selected",
▷   "In case ,the consumer services are delayed then penalty shall be beared."]  

▶ input.map { |line| line =~ /(?!.*delayed)(consumer|services)/ }
#⇒ [21, nil]
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160