vb.net - RegEx To Match "whole word" returns exception -


I am trying to validate via RegEx as follows ...

  If Regex.IsMatch (Output, "B \" and "Serial)" & amp; "\ B") then 'there is some end if  

but I get this argument exception

  parsing "\ bSerial) \ b" - lots of ) Of.  

I understand the error, but how do I modify the RegEx expression?

UPDATE The word "serial" dynamic It means I can get another exception for at least one more character for me.

Answers to 'bird and tanicus' correctly explains why your regex fails to compile .

But:

Even after avoiding brackets, be careful with your regex: \ b in only matches Word limits ( \ w one word made with the letters of the shortcut - letters, digits, and underscores), after the punctuation like parentheses no In your case the regex foo serial) will not match in the string like . It matches will foo serial) times , but only because \ b matches first Bar . Similarly, this string will not match the serial). .

In this way, it will not always be around a string with just \ b that you think

  Foo serial) bar foo (serial) bar:  

Edit: If, according to your comment below, in the list below ... FU serial) Bar FU serial))) )) FU serial)

... should be only the first and fifth matches, I tell that the rule matches the whole word, only if

  If Regex.IsMatch (Output, "(? & Lt; = ^ |) Use / first  
 . \ S) "& amp; Regex.Escape ("Serial)") & amp;  

However, it will no longer match foo with does not match . > or he said "foo" if you want to allow it, then

  if Regex.IsMatch ("output," (? & Lt; = ^ | \ B | \ s) "and use Regex.Escape (" serial ") & Amp; "(? = \ S | \ b | $)") then  

... but it will now match the second example. Choose your weapon carefully :)

(Explanation: (? & Lt; = | | \ b | \ s) is a positive perspective, which matches, string The beginning of a word boundary or the character of a white spot just before the current location, not adding anything to the outcome of the match. (? = \ S | \ b | $) Its lookimer equivalent Is.)


Comments