Zero-Width Assertions

You can place place conditions on what should occur before or after a match, through lookbehind, lookahead, anchors, and word boundaries.

These are called zero-width assertions.

ExpressionMeaning
^Start of string (or line in multiline mode)
$End of string (or line in multiline mode)
\AStart of string (ignores multiline mode)
\zEnd of string (ignores multiline mode)
\ZEnd of line or string
\GWhere search started
\bOn a word boundary
\BNot on a word boundary
(?=expr)Continue matching only if expression expr matches on right (positive lookahead)
(?!expr)Continue matching only if expression expr doesn't match on right (negative lookahead)
(?<=expr) Continue matching only if expression expr matches on left (positive lookbehind)
(?<!expr) Continue matching only if expression expr doesn't match on left (negative lookbehind)
(?>expr)Subexpression expr is matched once and not backtracked

Lookahead and Lookbehind

The (?=expr) construct checks whether the text that follows matches expr, without including expr in the result.

This is called positive lookahead.

In the following example, we look for a number followed by the word "miles":


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
       Console.WriteLine (Regex.Match ("25 miles", @"\d+\s(?=miles)")); 
    }
}

The output:


25

The word "miles" was not returned in the result, even though it was required to satisfy the match.

If we append .* to our expression as follows:


using System;
using System.Text.RegularExpressions;


class Program
{
    static void Main(string[] args)
    {
       Console.WriteLine (Regex.Match ("25 miles more", @"\d+\s(?=miles).*")); 
    }
}

The output:


25 miles more

Use Lookahead to validate a strong password.

Suppose a password has to be at least six characters and contain at least one digit.


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
         string password = "..."; 
         bool ok = Regex.IsMatch (password, @"(?=.*\d).{6,}"); 
    }
}

(?!expr) is the negative lookahead construct.

This requires that the match not be followed by expr.


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {

          string regex = "(?i)C#(?!.*(XML|Java))"; 
          Console.WriteLine (Regex.IsMatch ("C#! Java...",              regex));    
          Console.WriteLine (Regex.IsMatch ("C#! SQL!", regex));                
    }
}

The output:


False
True

(?<=expr) denotes positive lookbehind and requires that a match be preceded by a specified expression.

(?<!expr), denotes negative lookbehind and requires that a match not be preceded by a specified expression.

 
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        string regex = "(?i)(?<!Java.*)Sql"; 
        Console.WriteLine (Regex.IsMatch ("Java, Sql...", regex)); 
        Console.WriteLine (Regex.IsMatch ("Java C#!", regex));     
    }
}
  

The output:


False
False
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.