PregMatch

From SCAR Divi Manual
Jump to: navigation, search

Definition

function PregMatch(const Pattern, Subject: string): string;

Availability

SCAR Divi 3.25 > Current

Description

Matches a regular expression defined by Pattern in a string defined by Subject and returns the first match. The function is modeled after the equivalent which is available in PHP. An extended function with additional functionality is available as PregMatchEx.

Patterns

Regular expressions have to be defined in a specific way to be used with this function, the syntax is similar/the same as in PHP. The function allows you define modifiers (options) for the regex engine in the pattern string.

Patterns are defined as: /pattern/modifiers or @pattern@modifiers

Modifiers[1]

  • i: If this modifier is set, letters in the pattern match both upper and lower case letters.
  • m: By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline. This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.
  • s: If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
  • x: If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.
  • A: If this modifier is set, the pattern is forced to be "anchored", that is, it is constrained to match only at the start of the string which is being searched (the "subject string"). This effect can also be achieved by appropriate constructs in the pattern itself, which is the only way to do it in Perl.
  • S: When a pattern is going to be used several times, it is worth spending more time analyzing it in order to speed up the time taken for matching. If this modifier is set, then this extra analysis is performed. At present, studying a pattern is useful only for non-anchored patterns that do not have a single fixed starting character.
  • U: This modifier inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by ?. It is not compatible with Perl. It can also be set by a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).

Example 1

var
  s: string;

begin
  s := PregMatch('/\d+/', 'Hello World! 12345');
  WriteLn(s);
end.

Output:

12345

Example 2 (Using a modifier)

var
  s: string;

begin
  s := PregMatch('/hello/i', 'Hello World!');
  WriteLn(s);
end.

Output:

Hello

See Also

References

<references>