Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't match string using regular expression with "?"

Tags:

c#

regex

I need to build regular expression to match following strings:

  1. CCN
  2. CreditCardNum
  3. CreditCardNUmber
  4. CCNumber

etc.

I built it as follows : C(redit)?C(ard)?N(um|umber)?
It doesn't match "CreditCardNumber" string.
I also tried : C(redit)?C(ard)?N(:?um|umber) without success

like image 704
Yakov Avatar asked Jan 11 '23 03:01

Yakov


1 Answers

Your pattern is good, all you need to add is: (?i) at the begining

or IgnoreCase in the regex options. RegexOptions.IgnoreCase

Note: since you don't need to capture "redit" or "ard", non capturing groups (?:...) are better:

(?i)C(?:redit)?C(?:ard)?N(?:um(?:ber)?)?

If you want to have more control with the case:

C(?i:redit)?C(?i:ard)?N(?i:um(?:ber)?)?

For more security you can add word boundaries at the begining and at the end of the pattern \b

like image 175
Casimir et Hippolyte Avatar answered Jan 26 '23 22:01

Casimir et Hippolyte