Nika-Soft logo Nika-Soft logo

PBRegexp

Regular expression syntax

A regular expression has the following syntax
/Pattern/Options
/Pattern/Replacement/Options

Pattern

The pattern consists of metacharacters, which have special meaning, and ordinary characters, which match themselves. The pattern describes one or more strings to match when searching text.

Replacement

A string containing the text to replace for successful match of Pattern. Character escapes and substitutions are the only special constructs recognized in a replacement pattern.

Options

You can modify a regular expression pattern with options that affect matching behavior. For this purpose regular expressions can have various modifiers. Modifiers that relate to the interpretation of the regular expression are listed below.
i
Specifies case-insensitive matching.
s
Specifies single-line mode. Changes the meaning of the period character ( . ) to match any character whatsoever, even a newline, which normally it would not match.
m
Specifies multiline mode. Changes the meaning of ^ and $ so that they match at the beginning and end, respectively, of any line, not just the beginning and end of the whole string.
g
Specifies global replace mode. Indicates that a search should find and replace all occurrences of the pattern within the searched string, not just the first one.
x
Extend your pattern's legibility by permitting whitespace and comments.

Examples

/^\s*$/
Match a blank line.
/\b\w+\b/
Match a whole word.
/\b(\w+)\s+\1\b/i
Match a repeated word in any case.
/\d\d-\d\d-\d\d\d\d/
Match a date with format dd-mm-yyyy.
/(\d{1,2})\/(\d{1,2})\/(\d{4}|\d{2})/$2-$1-$3/
Change date format from dd/mm/yyyy to mm-dd-yyyy.
/\b(\w+)\b/($1)/g
Enclose any word with brackets.
/^\s*(.*?)\s*$/$1/s
Remove leading and trailing spaces from a string.
/^\s*(.*?)\s*$/$1/mg
Remove leading and trailing spaces from each line in a string.
Copyright © NikaSoft 2004-2005