Cannot match string using regular expression with character ??

I need to create a regular expression to match the following lines:

  • CCN
  • CreditCardNum
  • CreditCardNumber
  • CCNumber

and etc.

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

+4
source share
2 answers

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

or IgnoreCase in regex options. RegexOptions.IgnoreCase

Note: since you do not need to capture "redit" or "ard", it is better not to capture groups (?:...):

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

If you want more control over the case:

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

\b

+7

Try

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

(?i) , .

+2

Source: https://habr.com/ru/post/1528860/


All Articles