0

I want to filter the following strings (w/o quotes):

  • 'VISTA' <-- case doesn't matter here, can be VISTA, Vista, vista, visTA
  • '1+'
  • '1 +'

But not variations like

  • 'Vaddx'
  • 'Aista'
  • '1 +'
  • '1'
  • '1++'
  • '+1'

and so on

I'm using the following pattern for match, but I think something is missing.

 [VvIiSsTtAa]|1\\s\\+|1\\+

and the code bellow for test.

    static void Main(string[] args)
    {
        string[] paymentType = new string[] {"VISTA", "vista", "Vista", "vtid", "1", "1+", "1  +", "+", "+1", "1++"};

        foreach (var item in paymentType)
        {
            Console.Write($"Item {item} is ");

            if(!Regex.Match(item, "[VvIiSsTtAa]|1\\s\\+|1\\+").Success)
               Console.Write("not ");
            Console.WriteLine("valid.");
        }

        Console.WriteLine("\n\n\n");
    }

What I've got is:

- Item VISTA is valid
- Item vista is valid
- Item Vista is valid
- Item vtid is valid   <<---- WRONG
- Item 1 is not valid
- Item 1+ is valid
- Item 1  + is not valid
- Item + is not valid
- Item +1 is not válid
- Item 1++ is valid  <<---- WRONG

What is missing in my patter string?

Thanks in advance.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Valmir Cinquini
  • 323
  • 5
  • 16

1 Answers1

2

This is the simple approach, where (?i) stands for case insensitive:

(?i)^(vista|1[ ]?\+)$

Assumes boundary are the string anchors.