Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to match a string not containing a substring using RegEx in VB.NET

1 Answer

0 votes
Imports System
Imports System.Text.RegularExpressions

' ^: Matches the beginning of the string
' (?!notype): asserts that the current position in the string is not followed by the 
'             substring "notype"
' .: Matches any single character (except newline characters)
' *: Matches the preceding group (((?!notype).)) zero or more times
' $: Matches the end of the string

Public Class Program
    Public Shared Function StringNotContainingSubstring(ByVal pattern As String, ByVal text As String) As Boolean
        Dim re As Regex = New Regex(pattern, RegexOptions.IgnoreCase)
		
        Return re.IsMatch(text)
    End Function

	Public Shared Sub Main()
        Dim pattern As String = "^((?!notype).)*$"
		
        Console.WriteLine(StringNotContainingSubstring(pattern, " notype"))
        Console.WriteLine(StringNotContainingSubstring(pattern, "notype "))
        Console.WriteLine(StringNotContainingSubstring(pattern, "notypevar"))
        Console.WriteLine(StringNotContainingSubstring(pattern, "anotypevar"))
        Console.WriteLine(StringNotContainingSubstring(pattern, "anotype"))
        Console.WriteLine(StringNotContainingSubstring(pattern, "The only approval you need is your own"))
		Console.WriteLine(StringNotContainingSubstring(pattern, "Follow your dreams"))
        Console.WriteLine(StringNotContainingSubstring(pattern, "Never regret anything that made you smile"))
        Console.WriteLine(StringNotContainingSubstring(pattern, "A programming language"))
    End Sub
End Class



' run:
'
' False
' False
' False
' False
' False
' True
' True
' True
' True
' 

 



answered Feb 27, 2025 by avibootz

Related questions

...