Oct 5, 2009

Finding Blank Lines using regular expressions

Finding Blank Lines using regular expressions

In this regular expression we are going to find out blank lines. Means we are going to search lines containing only spaces (or Whitespace) and end of line.
Regular Expression Pattern

^\s*$
A description of the regular expression:

Beginning of line or string
Whitespace, any number of repetitions
End of line or string
How It Works

This regular expression will check for blank lines contains only spaces (Whitespace). Here we are going to search Beginning of line followed by Whitespace, any number of repetitions and End of line or string.

Finding Blank Lines using regular expressions



ASP.NET

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>



Finding Blank Lines using regular expressions


Valid Format: enter some spaces (or Whitespace)











C#.NET

//use System.Text.RegularExpressions befour using this function
public bool vldRegex(string strInput)
{
//create Regular Expression Match pattern object
Regex myRegex = new Regex("^\\s*$");
//boolean variable to hold the status
bool isValid = false;
if (string.IsNullOrEmpty(strInput))
{
isValid = false;
}
else
{
isValid = myRegex.IsMatch(strInput);
}
//return the results
return isValid;
}



VB.NET

‘Imports System.Text.RegularExpressions befour using this function
Public Function vldRegex(ByVal strInput As String) As Boolean
‘create Regular Expression Match pattern object
Dim myRegex As New Regex("^\s*$")
‘boolean variable to hold the status
Dim isValid As Boolean = False
If strInput = "" Then
isValid = False
Else
isValid = myRegex.IsMatch(strInput)
End If
‘return the results
Return isValid
End Function