3.2. Import the Regular Expression Library

Problem

To be able to use regular expressions in your application, you want to import the regular expression library or namespace into your source code.

Tip

The remainder of the source code snippets in this book assume that you have already done this, if needed.

Solution

C#

using System.Text.RegularExpressions;

VB.NET

Imports System.Text.RegularExpressions

XRegExp

For JavaScript code running in a browser:

<script src="xregexp-all-min.js"></script>

For JavaScript code running on a server using Node.js:

var XRegExp = require('xregexp').XRegExp;

Java

import java.util.regex.*;

Python

import re

Discussion

Some programming languages have regular expressions built-in. For these languages, you don’t need to do anything to enable regular expression support. Other languages provide regular expression functionality through a library that needs to be imported with an import statement in your source code. Some languages don’t have regex support at all. For those, you’ll have to compile and link in the regular expression support yourself.

C#

If you place the using statement at the top of your C# source file, you can reference the classes that provide regular expression functionality directly, without having to fully qualify them. For instance, you can write Regex() instead of System.Text.RegularExpressions.Regex().

VB.NET

If you place the Imports statement at the top of your VB.NET source file, you can reference the classes that provide regular expression functionality directly, ...

Get Regular Expressions Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.