RegEx - Find a word in String


   RegEx - Match line that does not contain a Word

Assume the below scenario with a set of string as follows:

  • Venkat
  • Jln
  • Wikitechy
  • John

A pattern must be applied to fetch John from the above sequence  of strings. Below follows the code to achieve the same:

Sample Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Regex_FirstExample	
{ 
    class Program	    
    {
        static void Main(string[] args)	       
        {
            string[] sentences =      
            {
                "Venkat",
                "Jln",
                "Wikitechy",
                "John"
            }; /// Input array of strings to validate
            string pattern = "^((?!John).)*$"; 
            // to check a particular word in regex
            foreach (string s in sentences)
            {/// wikitechy code
                if (System.Text.RegularExpressions.Regex.IsMatch(s, pattern))
                // regex to match line that doesn't contain a word?
                {
                    System.Console.WriteLine("The text string is : " + s);
                }
                else	
                {             
                }
            }
            // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();     } }

Code Explanation :

    The libraries are imported by default in C# class file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Here System is the parent library for C# code. Collections. Generic allows to generate strongly typed collections for better performance. The System.Text namespace is a holder for classes to represent ASCII and Unicode character encodingsRegularExpressions is a child class of System.Text.

    The code namespace Regex_FirstExample defines a holder to contain    one or more class.

    The line of code static void Main(string[] args) defines a static method    called main, whose objects can be called without an instance. It contains a string[] as parameter, to receive input from the console window.


    The line of code below declares a string array with a sequence of strings as follows:

string[] sentences = 
{
    "Venkat",
    "Jln",
    "Wikitechy",
    "John"
}; // Input array of strings to validate

    The code here declares a string variable with a string pattern to find within the above sequence as follows:

string pattern = "^((?!John).)*$";  // to check for a word   in  regex

    The for loop introduces a code to analyze each and every string present within the string array as follows:

foreach (string s in sentences)
{ /// wikitechy code

    Below shown if loop code checks whether the fetched string confirms a pattern match with the filter defined by the variable “pattern”. When the test returns true, the code System.Console.WriteLine("The text string is : " + s); outputs that string.

if (System.Text.RegularExpressions.Regex.IsMatch(s, pattern))
// regex to match line that doesn't contain a word?
{
    System.Console.WriteLine("The text string is : " + s);
}
else
{                    
}
}

Output :


    The filter “^((?!John).)*$" defines as follows : 

Pattern

Meaning

!John

The word “John” should not be present.

 

–?!John

Other than “John” and the word can be present zero or more times.

(?!John).

A dot can be present near the word other than “John” and the word can be present zero or more times.

^((?!John).)*$

“John” should not be there at start(^) or end($) of the string and a dot with any other word can be present zero or more times.

  • For loop first analyses, the string “Venkat” from the input. Since it satisfies filter defined in if loop, the output is written to console as 

        The text string is : Venkat

  • Similar test satisfies for strings “Jln” and “Wikitechy” and hence the output in console is 
        The text string is : Jln
        The text string is : Wikitechy  
  • Since the string “John” does not match the filter, it is not shown in output.

Workshop

Bug Bounty
Webinar

Join our Community

Advertise