⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06_03.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 6.2. Matching Letters (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:33:39Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch06_01.htm"TITLE="6. Pattern Matching"><LINKREL="prev"HREF="ch06_02.htm"TITLE="6.1. Copying and Substituting Simultaneously"><LINKREL="next"HREF="ch06_04.htm"TITLE="6.3. Matching Words"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_02.htm"TITLE="6.1. Copying and Substituting Simultaneously"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 6.1. Copying and Substituting Simultaneously"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch06_01.htm"TITLE="6. Pattern Matching"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_04.htm"TITLE="6.3. Matching Words"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 6.3. Matching Words"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch06-27738">6.2. Matching Letters</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-301">Problem<ACLASS="indexterm"NAME="ch06-idx-1000007523-0"></A><ACLASS="indexterm"NAME="ch06-idx-1000007523-1"></A><ACLASS="indexterm"NAME="ch06-idx-1000007523-2"></A></A></H3><PCLASS="para">You want to see whether a value only consists of alphabetic characters.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-307">Solution</A></H3><PCLASS="para">The obvious character class for matching regular letters isn't good enough in the general case:</P><PRECLASS="programlisting">if ($var =~ /^[A-Za-z]+$/) {    # it is purely alphabetic}</PRE><PCLASS="para">That's because it doesn't respect the user's locale settings. If you need to match letters with diacritics as well, <CODECLASS="literal">use</CODE> <CODECLASS="literal">locale</CODE> and match against a negated character class:</P><PRECLASS="programlisting">use locale;if ($var =~ /^[^\W\d_]+$/) {    print &quot;var is purely alphabetic\n&quot;;}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-329">Discussion</A></H3><PCLASS="para">Perl can't directly express "something alphabetic" independent of locale, so we have to be more clever. The <CODECLASS="literal">\w</CODE> regular expression notation matches one alphabetic, numeric, or underscore character. Therefore, <CODECLASS="literal">\W</CODE> is not one of those. The negated character class <CODECLASS="literal">[^\W\d_]</CODE> specifies a byte that must not be an alphanumunder, a digit, or an underscore. That leaves us with nothing but alphabetics, which is what we were looking for.</P><PCLASS="para">Here's how you'd use this in a program:</P><PRECLASS="programlisting">use locale;use POSIX 'locale_h';# the following locale string might be different on your systemunless (setlocale(LC_ALL, &quot;fr_CA.ISO8859-1&quot;)) {    die &quot;couldn't set locale to French Canadian\n&quot;;}while (&lt;DATA&gt;) {    chomp;    if (/^[^\W\d_]+$/) {        print &quot;$_: alphabetic\n&quot;;    } else {        print &quot;$_: line noise\n&quot;;    }}__END__sillyfa鏰deco鰌erateni駉Ren閑Moli鑢eh鎚oglobinna飗etsch

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -