1

I am using C# to search a paragraph and see if it contains a sentence like this: "... ... The amount $5,234.12 does not match our record. Please ... ..."

What I am interested is the sentence "The amount [whatever] does not match our record." However, how do I make the dollar amount a wildcard? I don't really care what the dollar amount is, I just want to check this sentence pattern exists in the paragraph.

Should I use RegEx? How? I would hate to just search for "The amount", and then followed by "does not match our record". Feel like there is a better way. Thanks!

Franky
  • 1,262
  • 2
  • 16
  • 31
  • If you care about order maybe regex or you can check if your string contains 2 strings, the first and the second part of the sentence ignoring the amount. Check if this helps https://stackoverflow.com/questions/3519539/how-to-check-if-a-string-contains-any-of-some-strings. Just to specify, in the link above the condition is if string contains any, in your case you would need to check if string contains both. – Vergil C. Apr 28 '20 at 00:00

1 Answers1

2

Simplest regex for this:

The amount .+ does not match our record.

The problem with the above is that .+ might match anything, including false positive, for instance "The amount", followed by a lot of words, and then an unrelated "does not match our record".

If such edge case could happen, you can use some more restrictions on the regex : limit number of characters and/or restrain the possible characters :

Limit number of characters : the 'amount' is between 2 characters ($1) and, let's say, 20 characters:

The amount .{2,20} does not match our record

Limit possible characters if you want to make sure the amount pattern contains only digits, dollar sign, dot and comma:

The amount [0-9$,.]+ does not match our record

You can also mix both :

The amount [0-9$,.]{2,20} does not match our record

Test it online on RegexStorm

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • side note : it may be also better on a performance point of view to restrict the regex, that would prevent useless _backtrackings_ – Pac0 Apr 28 '20 at 15:59