Trailing Modifiers

After the last delimiter in your regular expression you can place a number of optional modifier characters that affect how the expression operates. Table 7-1 lists the more commonly used ones.

Table 7-1. Regular expression modifiers

Modifier

Description

/g

Make the match global; that is, keep looking for additional matches after the first one has been found.

/i

Match case-insensitively, such that the search pattern /walnuts/i would successfully match Walnuts and WALNUTS and so on.

/m

Treat the string as multiple lines. More specifically, let the ^ beginning-of-string anchor and the $ end-of-string anchor match at the beginning or end (respectively) of internal lines within the search string. Without the /m modifier, these anchors would match only at the beginning or end of the entire string.

/o

Compile the regular expression search pattern only once. If the search pattern contains any variables whose contents change after the first time the expression is compiled, those changes won’t be reflected in the regular expression. In effect, the /o modifier is a promise by you to Perl that any variables in the search pattern are not going to change during the time the script is running.

/s

Treat the string as a single line, even if it isn’t. Specifically, let the special character . (dot), which normally matches any character except a newline, match newline characters embedded in the string.

/x

Allow the use of regular expression ...

Get Perl for Web Site Management 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.