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, without having to fully qualify them. For instance, you can write Regex() instead of System.Text.RegularExpressions.Regex().

Java

You have to import the java.util.regex package into your application to be able to use Java’s built-in regular expression library.

JavaScript

JavaScript’s regular expression support is built-in and always available.

XRegExp

If you want to use XRegExp to extend JavaScript’s regular expression syntax, your web page will need to load the XRegExp library. The easiest way to do that is to load xregexp-all-min.js which includes all of XRegExp’s functionality in minimized form. The XRegExp recipes in this book assume you’re doing just that.

If you’re concerned about page loading times and you do not use Unicode categories, blocks, and/or scripts, you can load the base library xregexp-min.js and load the addon libraries as needed. Load unicode-base.js to enable the p{} syntax for Unicode properties. You can then load unicode-blocks.js, unicode-categories.js, and/or unicode-scripts.js to make it possible to match Unicode blocks, categories, and/or scripts with p{}.

If you are using Node.js to run JavaScript on a server, then you’ll need to install XRegExp as an npm package. This can be done by entering npm install xregexp on the command line. Once installed, your server-side scripts can import the XRegExp library as shown in the Solution section.

PHP

The preg functions are built-in and always available in PHP 4.2.0 and later.

Perl

Perl’s regular expression support is built-in and always available.

Python

You have to import the re module into your script to be able to use Python’s regular expression functions.

Ruby

Ruby’s regular expression support is built-in and always available.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset