-1

I would like to conduct regex substitution. Here is the pattern I am using:

.*?fee.*?$|.*?charge.*?$

The matches the desired lines

  • "fees credit card"
  • "charges for interest"

However, it is also matching on coffee and feeder (I want to be specific that it does not match "coffee" or "feed" lines, how can I specifically prevent these matches but still handle cases like fee, fees)

  • "coffee shop"
  • feeder cattle

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69
Karun
  • 561
  • 7
  • 23

2 Answers2

2

You could use an alternation with 2 word boundaries \b to prevent the words being part of a larger word.

For you example data, if you want to match the single or single or plural version you can make the s at the end optional by using a question mark.

^.*\b(?:fees?|charges?)\b.*$
  • ^ Start of the string
  • .*\b Match any char except a newline followed by a word boundary
  • (?:fees?|charges?) Match any of the listed followed by an optional s
  • \b.* Word boundary, match any char except a newline 0+ times
  • $ Assert end of the string

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

If you are just trying to match those two lines, you can simply use an expression similar to this:

^(fees|charges).+$

enter image description here

If you wish to match certain words, you might add boundaries to group one similar to this expression:

^\b(fees|fee|charge|charges)\b(.+)$

enter image description here

If your pattern might be in the middle of string inputs, you can add another group in the left, similar to this expression:

(?:.+|)\b(fees|fee|charge|charges)\b(?:.+|)$

enter image description here

This graph shows how an expression like that would work:

enter image description here

Regular expression design can be achieved much easier, if/when there is real data.

Emma
  • 27,428
  • 11
  • 44
  • 69