Perl Best Practices (excerpt)---12

Source: Internet
Author: User
Tags alternation character classes xms

The 12th chapter: Regular Expressions

A regular expression is a sub-program. is the text matching subroutine. Yi Wu.

Be sure to use the/X flag. Yi Lu.

be sure to mark with/M. One restaurant Qi.

use \a and \z as the string boundary anchor point (anchor).

#删除前后空白 ...

$text =~ s{\a \s* | \s* \z}{}gxm; One restaurant Ba.

use \z to denote "end of string" and do not use \z.

Perl provides a variant version of the \z marker symbol: \z. However, lowercase \z refers to "match the end of a string", while uppercase \z refers to "match an optional newline character and then the end of the string." One of the nine.

always use the/s flag. Wu 0.

consider forcing the use of the Regexp::autoflags module.

In order for us to enter "/XMS" after each regular, we can use:

Useregexp::autoflags; One wood.

prefer to use m{...}, less in multi-line regular expressions with/.../. one Wu.

except/.../or m{...} , do not use other delimiters. Woo Jun.

It is best to use the character class without escaping the Metacharacters (metacharacter). Wu.

It is best to use a moniker without escaping the metacharacters.

For example:

if ($escape _seq =~/\177 \006 \030 z/xms) {#Octal del-ack-can-z

Blink (182);

}

Overwrite to:

Usecharnames QW (: Full);

if ($escape _seq =~ m/\n{delete} \n{acknowledge} \n{cancel} z/xms) {

Blink (182);

} Wu.

It is best to use the attribute instead of an enumerated character class.

Readonly my $ALPHA _ident = qr/\p{uppercase}\p{alphabetic}*/xms;

Note: Unicode is supported for versions above Perl5.8. Woolu.

consider matching any whitespace, rather than a specific whitespace character.

$config _line=~ m{($IDENT) \s* = \s* (. *)}xms Wu Qi.

when matching "as many" as possible, be sure to make it.

#格式为: <source>% <data> & <config> ...

if ($source =~ m/\a ([^%]*)% ([^&]*) & (. *)/xms) {

My ($statements, $data, $config) = ($, $, $);

My$prog = Compile ($statements, {config=> $config});

My$res = Execute ($prog, {data=> $data,config=> $config});

}

else{

Croak ' Invalid program ';

}

The last of the regular ". *?" It's not superfluous, it's not doing what you want to do, or you're forgetting a \z anchor point. Woo Jun.

Use the capture parentheses only when you want to capture it. Woo Jun.

use a numeric capture variable only if you are sure that the previous match was successful.

Numeric capture variable: $1,$2,$3 ... Lu 0.

Be sure to give the captured substring the appropriate name.

$myName = $; One Lu Yi.

use the/GC tag to tokenization the input string (tokenize).

The/GC tag tells the regular record where each successful match is completed; Yi Lu Ii.

use tables to create regular expressions.

# Establish a pattern to match any of the following irregular complex numbers ...

My$has_irregular_plural

=join ' | ', map {Quotemeta $_} reverse sort keys%irregular_plural_of; One continent three.

Complex regular expressions are constructed from simpler parts.

# Establish regular expressions to match floating-point numbers ...

Readonlymy $DIGITS = qr{\d+ (?: [.] \d*)?  | [.] \d+}xms;

readonlymy $SIGN = qr{[+]}xms;

readonlymy $EXPONENT = qr{[Ee] $SIGN? \d+}xms;

Readonly my $NUMBER =>qr{($SIGN?)  ($DIGITS) ($EXPONENT)} Xms

#稍后 ...

My (, $sign, $digits, $exponent)

= $input =~ $NUMBER; The one-land restaurant.

consider using Regexp::common and don't write your own regular expressions. Lu Wu.

using character classes, do not use a single character alternation (alternation).

if ($quotelike!~ m{\a (?: Q[QRX) | [Gsy] | TR) \z}xms) {

Carp "Unknown quotelike: $quotelike";

Nextquotelike;

} one already.

separate the common affixes from the alternating selection. Yi Lu Qi.

avoid useless backtracking. Yi Lu Ba.

It is best to compare the EQ of a fixed string with the regular expression of a fixed pattern.

# leave the command there are many variants of the version ...

Lastcommand if $cmd =~ m{\a (?: Q | Quit | Bye)}xms;

# leave the command there are many variants of the version ...

Lastcommand if $cmd eq ' q '

|| $cmd eq ' quit '

|| $cmd eq ' bye ';

Perl Best Practices (excerpt)---12

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.