A regular expression matches only two specific words, for example. Yes or no

My attempt:

^Yes|^No|^$ 

but when I use this, words other than "Yes" and "No" are matched

How to fix it?

I tested my regular expression with this online regular expression tester .

+6
source share
2 answers

Try the following:

 ^(?:Yes|No)$ 

In VBScript, something like this:

 Dim myRegExp, FoundMatch Set myRegExp = New RegExp myRegExp.Pattern = "^(?:Yes|No)$" FoundMatch = myRegExp.Test(SubjectString) 

What was the problem?

You had an alternation with three options:

  • ^Yes matches Yes at the beginning of the line, but also matches Yes in Yes, man...
  • ^No matches No at the beginning of the line, but also matches No in No way!
  • ^$ matches an empty string
+9
source

Below regex will match yes or no,

 ^(?:Yes\b|No\b) 

Demo

+3
source

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


All Articles