How to find a corresponding string using regex in text file

Here ABC is my keyword which appears thrice. I want to get corresponding “C"or"D” value as highlighted. C = credit, D= debit
Please help me with regex expression.

[C,D](?=\w+ABC)

This should work provided:

  • Your account number contains only letters or decimal digits
  • Your account number doesn’t contain the letters C or D

The logic: (?= )is a ‘lookbehind’ assertion. This regex looks for a string of characters - decimals and letters (\w+) to the left of the string “ABC” and matches results for “C” or “D”.

Hint: Next time, please paste that text as string so that we can use it to test the regex solution out.

1 Like

count how many chars are before the ABC and then use expression like:
.{4}ABC
your D or C will be like this: found.Value.Substring(0,1)

1 Like