<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    lqxue

    常用鏈接

    統(tǒng)計(jì)

    book

    tools

    最新評(píng)論

    2008年11月24日 #

    如何在 Windows 中刪除 ActiveX 控件

    刪除 ActiveX 控件,請(qǐng)按照相應(yīng)部分中的步驟操作。

    Internet Explorer 3.0、3.01 和 3.02

    1. 單擊“開始”,指向“設(shè)置”,單擊“控制面板”,雙擊“添加/刪除程序”,然后單擊“安裝/卸載”選項(xiàng)卡。
    2. 如果您要?jiǎng)h除的 ActiveX 控件出現(xiàn)在安裝的程序列表中,請(qǐng)單擊該 ActiveX 控件,單擊“添加/刪除”,然后按照屏幕上的說明操作。如果該 ActiveX 控件沒有出現(xiàn)在安裝的程序列表中,則繼續(xù)執(zhí)行下一步。
    3. 單擊“開始”,單擊“運(yùn)行”,在“打開”框中鍵入下列行,然后單擊“確定”:
      regsvr32 drive:\windows\occache\filename.ocx /u
      注意drive 是 Windows 文件夾所在的驅(qū)動(dòng)器號(hào),windows 是安裝 Windows 的文件夾名,而 filename.ocx 是您想要?jiǎng)h除的 ActiveX 控件。

      注意:如果您不知道要?jiǎng)h除的 ActiveX 控件 (.ocx) 的文件名,則可以通過查看安裝或使用 ActiveX 控件的網(wǎng)頁的超文本標(biāo)記語言 (HTML) 源文件來確定該文件名。要查看一個(gè)網(wǎng)頁的 HTML 源文件,請(qǐng)右鍵單擊該網(wǎng)頁的空白區(qū)域,然后單擊“查看源文件”。
    4. 在 Windows 資源管理器或 Windows NT 資源管理器中,單擊 Windows\Occache 文件夾中的 .ocx 文件,然后單擊“文件”菜單上的“刪除”。

    Occache 是在 Internet Explorer 3.x 的所有版本中安裝有 ActiveX 控件的文件夾的名稱。Regsvr32.exe 文件是由 Internet Explorer 安裝的,并且可用于注冊(cè)和刪除 ActiveX 控件的注冊(cè)表項(xiàng)。

    Internet Explorer 4.x 或更高版本(所有平臺(tái))

    Internet Explorer 4.x 或更高版本,包括 Occache.dll 文件,該文件可用于使用“shell 文件夾”枚舉、更新和安全地卸載 ActiveX 控件。

    1. 單擊“開始”,指向“設(shè)置”,單擊“控制面板”,雙擊“添加/刪除程序”,然后單擊“安裝/卸載”選項(xiàng)卡。
    2. 如果您要?jiǎng)h除的 ActiveX 控件出現(xiàn)在安裝的程序列表中,請(qǐng)單擊該 ActiveX 控件,單擊“添加/刪除”,然后按照屏幕上的說明操作。如果該 ActiveX 控件沒有出現(xiàn)在安裝的程序列表中,則繼續(xù)執(zhí)行下一步。
    3. 在 Windows 資源管理器或 Windows NT 資源管理器中,雙擊 Windows\Downloaded Program Files 文件夾或 Winnt\Downloaded Program Files 文件夾,右鍵單擊您想要?jiǎng)h除的 ActiveX 控件,然后單擊“刪除”。
    4. 在系統(tǒng)提示您是否刪除該 ActiveX 控件后,單擊“是”。

    重要說明:如果您運(yùn)行的是 Internet Explorer 4.0,則不應(yīng)刪除以下 ActiveX 控件:

    • DirectAnimation Java Classes
    • Internet Explorer Classes for Java
    • Microsoft XML Parser for Java
    • Win32 Classes

    Internet Explorer 5.0 或更高版本不要求 Downloaded Program Files 文件中的這些組件。

    刪除 ActiveX 控件時(shí)出現(xiàn)的錯(cuò)誤消息

    在您嘗試使用 Occache shell 文件夾刪除一個(gè) ActiveX 控件時(shí),可能顯示以下錯(cuò)誤消息之一:

    • 共享沖突 這些程序文件當(dāng)前正由一個(gè)或多個(gè)程序使用。請(qǐng)關(guān)閉一些程序,然后重試。您可能需要重新啟動(dòng) Windows。
    • 組件刪除 即將刪除 Windows 系統(tǒng) DLL:(<path\filename>)。是否刪除?

    共享沖突:

    如果您要嘗試刪除的 ActiveX 控件當(dāng)前在內(nèi)存中由 Internet Explorer 或“活動(dòng)桌面”組件加載,則顯示此錯(cuò)誤消息。

    要解決此錯(cuò)誤消息,請(qǐng)按照下列步驟操作:

    1. 關(guān)閉所有打開的 Internet Explorer 窗口。
    2. 禁用“活動(dòng)桌面”。為此,請(qǐng)右鍵單擊桌面上的空白區(qū)域,指向“活動(dòng)桌面”,然后單擊“查看網(wǎng)頁”以清除該復(fù)選標(biāo)記。
    3. 按照本文中前面部分的“Internet Explorer 4.0 或更高版本”部分中介紹的步驟,刪除該 ActiveX 控件。

      注意:您最好在刪除 ActiveX 控件前重新啟動(dòng) Windows。

    組件刪除:

    僅當(dāng)您要?jiǎng)h除的 ActiveX 控件向已注冊(cè)的 Occache 文件夾以外的文件夾(例如,Windows\System 或 Winnt\System32)安裝文件時(shí),該消息才出現(xiàn)在 4.01 Service Pack 1 (SP1) 之前的 Internet Explorer 4 版本中。Occache 不是總能確定這些文件是否正由其程序共享。

    如果您確定消息中顯示的一個(gè)或多個(gè)文件未由 Windows 或其他程序使用,請(qǐng)單擊“是”。否則,單擊“否”。

    注意:在 Internet Explorer 4.01 SP1 和更高版本中,Occache 不刪除(或提示您刪除)注冊(cè)的 Occache 文件夾之外的相關(guān)文件。

    支持多個(gè) Occache 文件夾

    Internet Explorer 4.0 和更高版本支持多個(gè) Occache 文件夾。Occache 文件夾的列表位于以下注冊(cè)表項(xiàng)中:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ActiveX Cache

    默認(rèn)情況下,Internet Explorer 4.0 和更高版本使用 Windows\Downloaded Program Files 或 Winnt\Downloaded Program Files 文件夾。如果是從 Internet Explorer 3.x 升級(jí)的,則 Occache 和 Downloaded Program Files 文件夾可能都存在。在此情況下,所有新的 ActiveX 控件都安裝在 Downloaded Program Files 文件夾中,但以前安裝的 ActiveX 控件仍在 Occache 文件夾中工作。當(dāng)您在 Windows 資源管理器、Windows NT 資源管理器或“我的電腦”中打開 Occache 或 Downloaded Program Files 文件夾時(shí),將顯示所有 ActiveX 控件,而與該 ActiveX 控件的文件所在的文件夾無關(guān)。在此情況下,注冊(cè)表項(xiàng)中將出現(xiàn)以下字符串值:

    "0"="C:\\WINDOWS\\OCCACHE"
    "1"="C:\\WINDOWS\\Downloaded Program Files"
    

    有關(guān) ActiveX 控件的其他信息,請(qǐng)參見 Microsoft 知識(shí)庫中的以下文章:

    154544  (http://support.microsoft.com/kb/154544/LN/ ) ActiveX 技術(shù)說明

    有關(guān) Internet Explorer 在發(fā)生沖突(例如,如果文件已存在)時(shí)如何下載 ActiveX 控件的信息,請(qǐng)參見 Microsoft 知識(shí)庫中的以下文章:

    196150  (http://support.microsoft.com/kb/196150/LN/ ) INFO:為什么會(huì)在代碼下載期間創(chuàng)建 CONFLICT 目錄

    posted @ 2010-06-08 16:16 lqx 閱讀(604) | 評(píng)論 (0)編輯 收藏

    A very cool PDF to Word converter.

    a  very cool pdf to word converter:http://www.pdftoword.com

    posted @ 2010-03-31 16:29 lqx 閱讀(232) | 評(píng)論 (0)編輯 收藏

    html 導(dǎo)出excel

         摘要:     <head>    <body>   <table class="dataintable" id='table22'>         <tbody><tr> &nb...  閱讀全文

    posted @ 2010-03-21 09:42 lqx 閱讀(336) | 評(píng)論 (0)編輯 收藏

    java 程序轉(zhuǎn)換成exe程序

    http://www.ej-technologies.com/products/exe4j/overview.html

    posted @ 2010-03-21 09:37 lqx 閱讀(262) | 評(píng)論 (2)編輯 收藏

    退出諾頓命令

    在運(yùn)行里執(zhí)行: smc -stop

    posted @ 2010-03-18 16:57 lqx 閱讀(314) | 評(píng)論 (0)編輯 收藏

    無提示框關(guān)閉瀏覽器

    window.open(....);
    this.focus();  self.opener = this;  self.close();

    posted @ 2010-03-15 14:36 lqx 閱讀(239) | 評(píng)論 (0)編輯 收藏

    IE foolscreen/全屏 瀏覽頁面

    There are two javascript ways to view web site in full screen mode in IE:

    1. window.open('http://www.sina.com.cn','','fullscreen')
       Note: If view the page remotely, this way will ineffective, and you can use the second one instead.
     

    2. var wsh=new ActiveXObject("wscript.shell");
       wsh.run("iexplore -k http://www.google.com");
     

    posted @ 2010-03-15 14:22 lqx 閱讀(216) | 評(píng)論 (0)編輯 收藏

    IE7: Automation server can't create object javascript

    問題描述:使用ie6導(dǎo)出excel表正常,但是在IE7 下總是報(bào)錯(cuò):“ Automation server can't create object javascript”。
     
    1. In Internet Explorer > Tools > Internet Options > Security > Custom Level
    2. Enabling or prompting "Initializing and Script Activex controls not marked as safe"



    3.IE7對(duì)環(huán)境的設(shè)置特別嚴(yán)格,常用到的有兩部分,一個(gè)是安全里面的“internet”設(shè)置,一個(gè)是“信任站點(diǎn)”的設(shè)置。

      1>需要把站點(diǎn)加入信任站點(diǎn)列表;
      2>設(shè)置“信任站點(diǎn)”項(xiàng)的“自定義級(jí)別”,啟用“對(duì)未標(biāo)記為可安全執(zhí)行 ActiveX控件初始化并執(zhí)行腳本”即可。

    posted @ 2010-03-15 14:14 lqx 閱讀(618) | 評(píng)論 (0)編輯 收藏

    Accecing a field on a tab navigator

    Default Accecing a field on a tab navigator

    hi,


    i have a tab navigator. in one of my functions i'm trying to reference a field which is located on the second tab.

    If i click "ok" from the first tab, which calls the function, which references the field on a second tab, it gives me a runtime error. But if i click on the second tab, and go back to the first, and then click on "OK" it works.

    What is going on? am i supposed to somehow declare taht field before referencing it? to make it active..?

    any help greatly appreciated.

    Thanks!


    this is the error i'm getting:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    MadamZuZu is offline   Reply With Quote
      #2

     
     
     
    Default

    The children of a TabNavigator aren't created until they're needed. In your case, only the contents of the first tab are actually created, because they're part of what's needed when the TabNavigator is first displayed. The contents of the second tab won't be instantiated until they need to be displayed, which is when the second tab is clicked on. That's why that error is being thrown until you click on the second tab. This process of only instantiating components as they are needed is called deferred instantiation.

    To force the contents of the other tabs to be instantiated, try changing the creationPolicy property of the TabNavigator to be "all". This will cause all of the different tabs to be instantiated, regardless of having been clicked on.

    In the Flex docs, check out Container.creationPolicy for more details.

    ActionScript Code:
                
    <mx:TabNavigator creationPolicy="all">

    posted @ 2010-03-14 11:34 lqx 閱讀(266) | 評(píng)論 (0)編輯 收藏

    用javascript操控css

    W3C DOM Compatibility - CSS
    From: http://www.quirksmode.org/dom/w3c_css.html#access


    posted @ 2010-03-12 10:58 lqx 閱讀(265) | 評(píng)論 (0)編輯 收藏

    CSS 控制打印

    From:http://martybugs.net/articles/print.cgi

    Web Page Printability With CSS
    author: Martin "mpot" Pot
    date: 7 January 2005


    Introduction

    This article describes how to use CSS media types to make your website much more printer-friendly. An overview of CSS media types is given, and sample HTML and CSS code is provided, detailing how to implement CSS media types for improving printability.

    Why?

    Most webpages do not print very well, with large amounts of the printed page being wasted with banners, navigation menus, advertising, and other unnecessary content. When attempting to print out the main text of a webpage, you often have to resort to copying the text into Word or another editor, and then printing it.

    Some websites make use of a link at the bottom of the page, linking to a "printable version" of the same page. However, this requires additional effort on the part of the web developer.

    There is a much easier way to make your pages print better, and that's via the use of CSS media types.

    For example, all content on MartyBugs.Net has been designed to be printer-friendly. When printing pages from this site using a modern browser, elements such as the left menu column and the right column (where used) will be hidden, thus ensuring there's more room on the printed page for the main content.

    page viewed on screen
    page as displayed on a 1024x768 screen

    The image above shows how one of the pages on this website would be displayed on a computer screen, at a resolution of 1024x768.

    printed page
    printed page, optimised with CSS media types
    printed page,
    no print optimisation

    printed page,
    optimised for printing

    The left image above shows how the same page will look when printed, if all the page content is printed. Notice how little room there is in the centre of the page for the main content.

    The right image above shows the same page, but the navigation and other content on the left and right hand sides of the page is not printed. CSS media types have been used to hide this content when the page is printed.

    CSS Media Types

    The primary aim of the CSS media types is to allow the web-page author to define different styles for a page, depending on the media being used to display the page. CSS 2.1 supports numerous media types, including all (suitable for all devices), screen (computer screens), print (pages viewed on-screen in print-preview mode and printed pages).
    Other less-common media types are also supported, such as braille, embossed, handheld, projection, speech, etc, but we won't be discussing any of these.

    CSS media types are defined using the rule in your CSS style-sheet, or in your in-line CSS style definitions. For example, to define different font sizes for the print and screen media types, as well as a common line-height to be used for both, the CSS definition is as follows:

      @media print {
    body { font-size: 10pt }
    }
    @media screen {
    body { font-size: 13px }
    }
    @media screen, print {
    body { line-height: 1.2 }
    }

    The CSS media types allow you to specify different CSS properties for any element, so it will be displayed differently on the screen than when printed. For the purposes of this article, the only thing we're interested in is to hide some page elements when the page is printed.

    Refer to the W3C CSS 2.1 Media Definitions for more details on CSS media types.

    Using CSS Media Types

    The web pages on this website are all generated on-the-fly, using server-side Perl templates. All pages have a number of common elements, namely:
    • a banner across the top
    • navigation menu on the left side
    • main content in the centre
    • advertising and other content on the right side
    • footer across the bottom
    When someone prints out a page from this website, CSS media types are used to hide a number of elements on the page, including the navigation menu, advertising, and the navigation content in the footer.

    To provide a working demonstration of how CSS media types can be used in this way, a demonstration web page was coded, using tables to define the banner area across the top of the page, with a column down the left-hand side for the navigation menu, a column down the right-hand side for other content, and a footer across the bottom of the page.

    This layout is one of the most commonly used page layouts on the internet at the present - hence my choice to use it as an example.

    the example page
    the example page

    Firstly, we define the CSS styles for the page:

      <style type="text/css">
    @media print {
    .noprint { display: none; }
    }

    </style>

    Note that the CSS styles can also be defined using a separate CSS file, but example page has the CSS defined in-line for simplicity (and to keep the example to a single file).

    The above CSS definition is defining a style called noprint, and will only be applied to printed content. Setting the display property to none means any content using this CSS style will not be displayed when printed, but will be displayed for all other media types.

    There are a number of components on this page that we don't want printed, namely the columns on the left-hand side and on the right-hand side. The noprint style is applied to the table cells holding this content, thus ensuring they won't be visible when the page is printed.

    <table border='1' cellpadding='4' cellspacing='0' width='100%' >
    <tr height='60'><td colspan='3'>
    banner place-holder
    </td></tr>
    <tr><td width='150' valign='top' class='noprint'>
    left-side content place-holder
    </td><td>
    <b>main content</b>
    [snip!]
    </td><td width='150' valign='top' class='noprint'>
    right-side content place-holder
    </td></tr><tr><td colspan='3'>
    footer content place-holder
    </td></tr>
    </table>

    This will result in the columns on the left and right-hand sides being hidden when the page is printed, thus providing more space on the printed page for the content itself.

    Note that this style needs to be applied to the table cells themselves, and not just to the content in these table cells, else the (empty) cells will still show up on the printed copy.

    Testing Your CSS

    The easiest way to test CSS styles for print media is to use a web browser which has print preview capabilities. The print preview will give you a fairly accurate representation of how the page will look when it is printed, and is a much more efficient way to test your media styles, rather than actually printing out pages on a printer.

    Most popular web browsers should support print preview functionality. The FireFox browser has print preview functionality, although it lacks any zoom/unzoom capabilities.

    As an alternative to using print preview, you can print the web page to a PDF file. There are numerous (costly, freeware, and anywhere in between) software packages for doing this. I recommend using the freeware and open-source PDFCreator, which is a Windows printer driver which allows you to create PDFs from any Windows application, just by selecting the PDFCreator driver as the virtual printer from that application.

    Also be sure to use the W3C's free CSS Validation Service to check your CSS definitions.

    References


    posted @ 2010-03-12 10:55 lqx 閱讀(558) | 評(píng)論 (0)編輯 收藏

    MS09-073: Description of the security update for Windows XP, Windows 2000, and Windows Server 2003: December 8, 2009

    http://support.microsoft.com/kb/973904

    Let me fix it myself

    To unregister the mswrd632 converter yourself, edit the registry as follows:
    1. Click Start, click Run, type regedit, and then click OK.
    2. Locate and then click the following registry subkey:
      • For 32-bit versions of Windows:
        HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Text Converters\Import\MSWord6.wpc
      • For 64-bit versions of Windows:
        HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Shared Tools\Text Converters\Import\MSWord6.wpc
    3. On the Edit menu, click Delete.
    4. Click Yes.
    5. Exit Registry Editor.
    This change will effectively unregister the converter and disable it for third-party applications and for Microsoft Office. Microsoft Office will use its own text converters to open these kinds of files.

    posted @ 2010-03-04 01:56 lqx 閱讀(171) | 評(píng)論 (0)編輯 收藏

    Fix “Word Cannot Start the Converter MSWRD632.WPC” Error

    The user was running Office 2007 and getting this error. After doing some research, we came across a few possible solutions. In this post, I’ll list them out here.

    Method 1 – Unregister Text Converter

    Basically, in Windows XP SP2 and above and Windows Server 2003 SP1 and above, if you have a plain text file or another kind of file that is not a Microsoft Word file, but has a .doc extension, you will get this error.

    This can also happen if you are opening a really old document created by Word for Windows 6.0 or Word 97 documents. In this case, you can disable the mswrd632 converter so you don’t get this error. The files will then be opened by the Microsoft Office text converters.

    You can unregister this converter by going to Start, then run and typing regedit. Then navigate to the following key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Text Converters\Import\MSWord6.wpc

    mswrd632 error word

    Right-click on the MSWord6.wpc key in the left hand pane and choose Delete. If you have a Word 97 document, you should still be able to open it in Word 2003 or 2007. However, you will not be able to open these files in WordPad anymore. You’ll get an error like:

    Cannot load Word for Windows 6.0 files

    If you really have to use WordPad to open Word 6.0/95 files, you can re-enable the Word 6.0/95 for Windows and Macintosh to RFT converter. Open the registry and go to the following keys:

    For 32-bit versions of Windows

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\ Applets\Wordpad

    For Microsoft Windows on Windows 64 (WOW) mode

    HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\ CurrentVersion\Applets\Wordpad

    If the Wordpad key is not there in the left-hand pane, create it. After that, create a new DWORD value in the right-pane called AllowConversion and give it a value of 1.

    Method 2 – Copy the MSWrd632.wpc File

    The other way to solve this issue is to copy the MSWrd632.wpc file from another computer to the computer having problems.

    The only issue with this is that it is less secure and makes your computer vulnerable to a possible security hack, so don’t do this unless you really have to or that computer is not connected to the Internet.

    You can usually find this file in the following path:

    C:\Program Files\Common Files\Microsoft Shared\TextConv\

    That’s about it! If you are still having this problem, post a comment here and I’ll try to help! Enjoy!

    posted @ 2010-03-04 01:53 lqx 閱讀(3335) | 評(píng)論 (2)編輯 收藏

    COM Add In

    詳解COM Add InLoadBehavior及其妙用
    http://blog.csdn.net/v_jzho/archive/2007/10/06/1813080.aspx

    基于Visual Studio 2003/2005的Office插件開發(fā)FAQ
    http://blog.csdn.net/tonyqus/archive/2007/02/24/1513256.aspx

    posted @ 2010-03-03 14:58 lqx 閱讀(146) | 評(píng)論 (0)編輯 收藏

    Deploy Assembly in GAC

    There are several ways to deploy an assembly into the global assembly
    cache:

    1) Use an installer designed to work with the global assembly cache. This
    is the preferred option for installing assemblies into the global assembly
    cache

    2) Use a developer tool called the Global Assembly Cache tool (Gacutil.exe)
    provided by the .NET Framework SDK.

    3) Use Windows Explorer to drag and drop assemblies into the cache.

    4) use install shield. (注意:installshield里只有msi等幾個(gè)類型的項(xiàng)目支持這個(gè))

    From:http://bytes.com/topic/net/answers/109942-deploy-assembly-gac

    posted @ 2010-03-03 10:12 lqx 閱讀(155) | 評(píng)論 (0)編輯 收藏

    2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies

    http://www.microsoft.com/downloads/details.aspx?familyid=59DAEBAA-BED4-4282-A28C-B864D8BFA513&displaylang=en
    http://msdn.microsoft.com/en-us/library/15s06t57(VS.80).aspx

    posted @ 2010-03-02 17:01 lqx 閱讀(740) | 評(píng)論 (1)編輯 收藏

    error -7104: The destination of the component AllOtherFiles

    The 'AllOtherFiles' is an entry automatically created when you check the GlobalAssemblyCache predefined folder. After you add in your file(s), perform the following steps:

    1. Right-click "Destination Computer"
    2. Check "Show Components"
    3. Expand the [GlobalAssemblyCache] tree.
    4. Delete the "AllOtherFiles" entry.

    posted @ 2010-03-02 14:22 lqx 閱讀(226) | 評(píng)論 (0)編輯 收藏

    正則表達(dá)式(regular expression)

    正則表達(dá)式(regular expression)

    關(guān)鍵字: 正則表達(dá)式,Regular Expression

    作者:笑容

    發(fā)表于:2004年05月03日
    最后更新:2005年01月17日 19:54
    版權(quán)聲明:使用
    創(chuàng)作公用版權(quán)協(xié)議

    引用地址:<a >正則表達(dá)式(regular expression)</a>

    NAV: 笑容的八小時(shí)外 / 笑容的八小時(shí)外資料索引

    如何創(chuàng)建一個(gè)網(wǎng)站 (HOW TO: Initiate a website) Red Hat Enterprise Linux 介紹


    前言

    正則表達(dá)式是煩瑣的,但是強(qiáng)大的,學(xué)會(huì)之后的應(yīng)用會(huì)讓你除了提高效率外,會(huì)給你帶來絕對(duì)的成就感。只要認(rèn)真去閱讀這些資料,加上應(yīng)用的時(shí)候進(jìn)行一定的參考,掌握正則表達(dá)式不是問題。

    索引

    1._引子
    2._正則表達(dá)式的歷史
    3._正則表達(dá)式定義

    3.1_普通字符
    3.2_非打印字符
    3.3_特殊字符
    3.4_限定符
    3.5_定位符
    3.6_選擇
    3.7_后向引用

    4._各種操作符的運(yùn)算優(yōu)先級(jí)
    5._全部符號(hào)解釋
    6._部分例子
    7._正則表達(dá)式匹配規(guī)則

    7.1_基本模式匹配
    7.2_字符簇
    7.3_確定重復(fù)出現(xiàn)


    1. 引子

      目前,正則表達(dá)式已經(jīng)在很多軟件中得到廣泛的應(yīng)用,包括*nix(Linux, Unix等),HP等操作系統(tǒng),PHP,C#,Java等開發(fā)環(huán)境,以及很多的應(yīng)用軟件中,都可以看到正則表達(dá)式的影子。

      正則表達(dá)式的使用,可以通過簡單的辦法來實(shí)現(xiàn)強(qiáng)大的功能。為了簡單有效而又不失強(qiáng)大,造成了正則表達(dá)式代碼的難度較大,學(xué)習(xí)起來也不是很容易,所以需要付出一些努力才行,入門之后參照一定的參考,使用起來還是比較簡單有效的。

    例子: ^.+@.+""..+$

      這樣的代碼曾經(jīng)多次把我自己給嚇退過。可能很多人也是被這樣的代碼給嚇跑的吧。繼續(xù)閱讀本文將讓你也可以自由應(yīng)用這樣的代碼。

      注意:這里的第7部分跟前面的內(nèi)容看起來似乎有些重復(fù),目的是把前面表格里的部分重新描述了一次,目的是讓這些內(nèi)容更容易理解。

    2. 正則表達(dá)式的歷史

      正則表達(dá)式的“祖先”可以一直上溯至對(duì)人類神經(jīng)系統(tǒng)如何工作的早期研究。Warren McCulloch 和 Walter Pitts 這兩位神經(jīng)生理學(xué)家研究出一種數(shù)學(xué)方式來描述這些神經(jīng)網(wǎng)絡(luò)。

      1956 年, 一位叫 Stephen Kleene 的數(shù)學(xué)家在 McCulloch 和 Pitts 早期工作的基礎(chǔ)上,發(fā)表了一篇標(biāo)題為“神經(jīng)網(wǎng)事件的表示法”的論文,引入了正則表達(dá)式的概念。正則表達(dá)式就是用來描述他稱為“正則集的代數(shù)”的表達(dá)式,因此采用“正則表達(dá)式”這個(gè)術(shù)語。

      隨后,發(fā)現(xiàn)可以將這一工作應(yīng)用于使用 Ken Thompson 的計(jì)算搜索算法的一些早期研究,Ken Thompson 是 Unix 的主要發(fā)明人。正則表達(dá)式的第一個(gè)實(shí)用應(yīng)用程序就是 Unix 中的 qed 編輯器。

      如他們所說,剩下的就是眾所周知的歷史了。從那時(shí)起直至現(xiàn)在正則表達(dá)式都是基于文本的編輯器和搜索工具中的一個(gè)重要部分。

    3. 正則表達(dá)式定義

      正則表達(dá)式(regular expression)描述了一種字符串匹配的模式,可以用來檢查一個(gè)串是否含有某種子串、將匹配的子串做替換或者從某個(gè)串中取出符合某個(gè)條件的子串等。

    • 列目錄時(shí), dir *.txt或ls *.txt中的*.txt就是一個(gè)正則表達(dá)式,因?yàn)檫@里*與正則式的*的含義是不同的。

      正則表達(dá)式是由普通字符(例如字符 a 到 z)以及特殊字符(稱為元字符)組成的文字模式。正則表達(dá)式作為一個(gè)模板,將某個(gè)字符模式與所搜索的字符串進(jìn)行匹配。

    3.1 普通字符

      由所有那些未顯式指定為元字符的打印和非打印字符組成。這包括所有的大寫和小寫字母字符,所有數(shù)字,所有標(biāo)點(diǎn)符號(hào)以及一些符號(hào)。

    3.2 非打印字符

    字符 含義
    "cx 匹配由x指明的控制字符。例如, "cM 匹配一個(gè) Control-M 或回車符。x 的值必須為 A-Z 或 a-z 之一。否則,將 c 視為一個(gè)原義的 'c' 字符。
    "f 匹配一個(gè)換頁符。等價(jià)于 "x0c 和 "cL。
    "n 匹配一個(gè)換行符。等價(jià)于 "x0a 和 "cJ。
    "r 匹配一個(gè)回車符。等價(jià)于 "x0d 和 "cM。
    "s 匹配任何空白字符,包括空格、制表符、換頁符等等。等價(jià)于 [ "f"n"r"t"v]。
    "S 匹配任何非空白字符。等價(jià)于 [^ "f"n"r"t"v]。
    "t 匹配一個(gè)制表符。等價(jià)于 "x09 和 "cI。
    "v 匹配一個(gè)垂直制表符。等價(jià)于 "x0b 和 "cK。

    3.3 特殊字符

    所謂特殊字符,就是一些有特殊含義的字符,如上面說的"*.txt"中的*,簡單的說就是表示任何字符串的意思。如果要查找文件名中有*的文件,則需要對(duì)*進(jìn)行轉(zhuǎn)義,即在其前加一個(gè)"。ls "*.txt。正則表達(dá)式有以下特殊字符。
    特別字符 說明
    $ 匹配輸入字符串的結(jié)尾位置。如果設(shè)置了 RegExp 對(duì)象的 Multiline 屬性,則 $ 也匹配 '"n' 或 '"r'。要匹配 $ 字符本身,請(qǐng)使用 "$。
    ( ) 標(biāo)記一個(gè)子表達(dá)式的開始和結(jié)束位置。子表達(dá)式可以獲取供以后使用。要匹配這些字符,請(qǐng)使用 "( 和 ")。
    * 匹配前面的子表達(dá)式零次或多次。要匹配 * 字符,請(qǐng)使用 "*。
    + 匹配前面的子表達(dá)式一次或多次。要匹配 + 字符,請(qǐng)使用 "+。
    . 匹配除換行符 "n之外的任何單字符。要匹配 .,請(qǐng)使用 "。
    [ 標(biāo)記一個(gè)中括號(hào)表達(dá)式的開始。要匹配 [,請(qǐng)使用 "[。
    ? 匹配前面的子表達(dá)式零次或一次,或指明一個(gè)非貪婪限定符。要匹配 ? 字符,請(qǐng)使用 "?。
    " 將下一個(gè)字符標(biāo)記為或特殊字符、或原義字符、或向后引用、或八進(jìn)制轉(zhuǎn)義符。例如, 'n' 匹配字符 'n'。'"n' 匹配換行符。序列 '""' 匹配 """,而 '"(' 則匹配 "("。
    ^ 匹配輸入字符串的開始位置,除非在方括號(hào)表達(dá)式中使用,此時(shí)它表示不接受該字符集合。要匹配 ^ 字符本身,請(qǐng)使用 "^。
    { 標(biāo)記限定符表達(dá)式的開始。要匹配 {,請(qǐng)使用 "{。
    | 指明兩項(xiàng)之間的一個(gè)選擇。要匹配 |,請(qǐng)使用 "|。
    •   構(gòu)造正則表達(dá)式的方法和創(chuàng)建數(shù)學(xué)表達(dá)式的方法一樣。也就是用多種元字符與操作符將小的表達(dá)式結(jié)合在一起來創(chuàng)建更大的表達(dá)式。正則表達(dá)式的組件可以是單個(gè)的字符、字符集合、字符范圍、字符間的選擇或者所有這些組件的任意組合。

    3.4 限定符

    限定符用來指定正則表達(dá)式的一個(gè)給定組件必須要出現(xiàn)多少次才能滿足匹配。有*或+或?或{n}或{n,}或{n,m}共6種。
    *、+和?限定符都是貪婪的,因?yàn)樗鼈儠?huì)盡可能多的匹配文字,只有在它們的后面加上一個(gè)?就可以實(shí)現(xiàn)非貪婪或最小匹配。
    正則表達(dá)式的限定符有:
    字符 描述
    * 匹配前面的子表達(dá)式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等價(jià)于{0,}。
    + 匹配前面的子表達(dá)式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等價(jià)于 {1,}。
    ? 匹配前面的子表達(dá)式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等價(jià)于 {0,1}。
    {n} n 是一個(gè)非負(fù)整數(shù)。匹配確定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的兩個(gè) o。
    {n,} n 是一個(gè)非負(fù)整數(shù)。至少匹配n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等價(jià)于 'o+'。'o{0,}' 則等價(jià)于 'o*'。
    {n,m} m 和 n 均為非負(fù)整數(shù),其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o{1,3}" 將匹配 "fooooood" 中的前三個(gè) o。'o{0,1}' 等價(jià)于 'o?'。請(qǐng)注意在逗號(hào)和兩個(gè)數(shù)之間不能有空格。

    3.5 定位符

    用來描述字符串或單詞的邊界,^和$分別指字符串的開始與結(jié)束,"b描述單詞的前或后邊界,"B表示非單詞邊界。不能對(duì)定位符使用限定符。

    3.6 選擇

    用圓括號(hào)將所有選擇項(xiàng)括起來,相鄰的選擇項(xiàng)之間用|分隔。但用圓括號(hào)會(huì)有一個(gè)副作用,是相關(guān)的匹配會(huì)被緩存,此時(shí)可用?:放在第一個(gè)選項(xiàng)前來消除這種副作用。
    其中?:是非捕獲元之一,還有兩個(gè)非捕獲元是?=和?!,這兩個(gè)還有更多的含義,前者為正向預(yù)查,在任何開始匹配圓括號(hào)內(nèi)的正則表達(dá)式模式的位置來匹配搜索字符串,后者為負(fù)向預(yù)查,在任何開始不匹配該正則表達(dá)式模式的位置來匹配搜索字符串。

    3.7 后向引用

    對(duì)一個(gè)正則表達(dá)式模式或部分模式兩邊添加圓括號(hào)將導(dǎo)致相關(guān)匹配存儲(chǔ)到一個(gè)臨時(shí)緩沖區(qū)中,所捕獲的每個(gè)子匹配都按照在正則表達(dá)式模式中從左至右所遇到的內(nèi)容存儲(chǔ)。存儲(chǔ)子匹配的緩沖區(qū)編號(hào)從 1 開始,連續(xù)編號(hào)直至最大 99 個(gè)子表達(dá)式。每個(gè)緩沖區(qū)都可以使用 '"n' 訪問,其中 n 為一個(gè)標(biāo)識(shí)特定緩沖區(qū)的一位或兩位十進(jìn)制數(shù)。
    可以使用非捕獲元字符 '?:', '?=', or '?!' 來忽略對(duì)相關(guān)匹配的保存。

    4. 各種操作符的運(yùn)算優(yōu)先級(jí)

    相同優(yōu)先級(jí)的從左到右進(jìn)行運(yùn)算,不同優(yōu)先級(jí)的運(yùn)算先高后低。各種操作符的優(yōu)先級(jí)從高到低如下:
    操作符 描述
    " 轉(zhuǎn)義符
    (), (?:), (?=), [] 圓括號(hào)和方括號(hào)
    *, +, ?, {n}, {n,}, {n,m} 限定符
    ^, $, "anymetacharacter 位置和順序
    | “或”操作

    5. 全部符號(hào)解釋

    字符 描述
    " 將下一個(gè)字符標(biāo)記為一個(gè)特殊字符、或一個(gè)原義字符、或一個(gè) 向后引用、或一個(gè)八進(jìn)制轉(zhuǎn)義符。例如,'n' 匹配字符 "n"。'"n' 匹配一個(gè)換行符。序列 '""' 匹配 """ 而 ""(" 則匹配 "("。
    ^ 匹配輸入字符串的開始位置。如果設(shè)置了 RegExp 對(duì)象的 Multiline 屬性,^ 也匹配 '"n' 或 '"r' 之后的位置。
    $ 匹配輸入字符串的結(jié)束位置。如果設(shè)置了RegExp 對(duì)象的 Multiline 屬性,$ 也匹配 '"n' 或 '"r' 之前的位置。
    * 匹配前面的子表達(dá)式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等價(jià)于{0,}。
    + 匹配前面的子表達(dá)式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等價(jià)于 {1,}。
    ? 匹配前面的子表達(dá)式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等價(jià)于 {0,1}。
    {n} n 是一個(gè)非負(fù)整數(shù)。匹配確定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的兩個(gè) o。
    {n,} n 是一個(gè)非負(fù)整數(shù)。至少匹配n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等價(jià)于 'o+'。'o{0,}' 則等價(jià)于 'o*'。
    {n,m} m 和 n 均為非負(fù)整數(shù),其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o{1,3}" 將匹配 "fooooood" 中的前三個(gè) o。'o{0,1}' 等價(jià)于 'o?'。請(qǐng)注意在逗號(hào)和兩個(gè)數(shù)之間不能有空格。
    ? 當(dāng)該字符緊跟在任何一個(gè)其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面時(shí),匹配模式是非貪婪的。非貪婪模式盡可能少的匹配所搜索的字符串,而默認(rèn)的貪婪模式則盡可能多的匹配所搜索的字符串。例如,對(duì)于字符串 "oooo",'o+?' 將匹配單個(gè) "o",而 'o+' 將匹配所有 'o'。
    . 匹配除 ""n" 之外的任何單個(gè)字符。要匹配包括 '"n' 在內(nèi)的任何字符,請(qǐng)使用象 '[."n]' 的模式。
    (pattern) 匹配 pattern 并獲取這一匹配。所獲取的匹配可以從產(chǎn)生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中則使用 $0…$9 屬性。要匹配圓括號(hào)字符,請(qǐng)使用 '"(' 或 '")'。
    (?:pattern) 匹配 pattern 但不獲取匹配結(jié)果,也就是說這是一個(gè)非獲取匹配,不進(jìn)行存儲(chǔ)供以后使用。這在使用 "或" 字符 (|) 來組合一個(gè)模式的各個(gè)部分是很有用。例如, 'industr(?:y|ies) 就是一個(gè)比 'industry|industries' 更簡略的表達(dá)式。
    (?=pattern) 正向預(yù)查,在任何匹配 pattern 的字符串開始處匹配查找字符串。這是一個(gè)非獲取匹配,也就是說,該匹配不需要獲取供以后使用。例如,'Windows (?=95|98|NT|2000)' 能匹配 "Windows 2000" 中的 "Windows" ,但不能匹配 "Windows 3.1" 中的 "Windows"。預(yù)查不消耗字符,也就是說,在一個(gè)匹配發(fā)生后,在最后一次匹配之后立即開始下一次匹配的搜索,而不是從包含預(yù)查的字符之后開始。
    (?!pattern) 負(fù)向預(yù)查,在任何不匹配 pattern 的字符串開始處匹配查找字符串。這是一個(gè)非獲取匹配,也就是說,該匹配不需要獲取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配 "Windows 3.1" 中的 "Windows",但不能匹配 "Windows 2000" 中的 "Windows"。預(yù)查不消耗字符,也就是說,在一個(gè)匹配發(fā)生后,在最后一次匹配之后立即開始下一次匹配的搜索,而不是從包含預(yù)查的字符之后開始
    x|y 匹配 x 或 y。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 則匹配 "zood" 或 "food"。
    [xyz] 字符集合。匹配所包含的任意一個(gè)字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。
    [^xyz] 負(fù)值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。
    [a-z] 字符范圍。匹配指定范圍內(nèi)的任意字符。例如,'[a-z]' 可以匹配 'a' 到 'z' 范圍內(nèi)的任意小寫字母字符。
    [^a-z] 負(fù)值字符范圍。匹配任何不在指定范圍內(nèi)的任意字符。例如,'[^a-z]' 可以匹配任何不在 'a' 到 'z' 范圍內(nèi)的任意字符。
    "b 匹配一個(gè)單詞邊界,也就是指單詞和空格間的位置。例如, 'er"b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'。
    "B 匹配非單詞邊界。'er"B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'。
    "cx 匹配由 x 指明的控制字符。例如, "cM 匹配一個(gè) Control-M 或回車符。x 的值必須為 A-Z 或 a-z 之一。否則,將 c 視為一個(gè)原義的 'c' 字符。
    "d 匹配一個(gè)數(shù)字字符。等價(jià)于 [0-9]。
    "D 匹配一個(gè)非數(shù)字字符。等價(jià)于 [^0-9]。
    "f 匹配一個(gè)換頁符。等價(jià)于 "x0c 和 "cL。
    "n 匹配一個(gè)換行符。等價(jià)于 "x0a 和 "cJ。
    "r 匹配一個(gè)回車符。等價(jià)于 "x0d 和 "cM。
    "s 匹配任何空白字符,包括空格、制表符、換頁符等等。等價(jià)于 [ "f"n"r"t"v]。
    "S 匹配任何非空白字符。等價(jià)于 [^ "f"n"r"t"v]。
    "t 匹配一個(gè)制表符。等價(jià)于 "x09 和 "cI。
    "v 匹配一個(gè)垂直制表符。等價(jià)于 "x0b 和 "cK。
    "w 匹配包括下劃線的任何單詞字符。等價(jià)于'[A-Za-z0-9_]'。
    "W 匹配任何非單詞字符。等價(jià)于 '[^A-Za-z0-9_]'。
    "xn 匹配 n,其中 n 為十六進(jìn)制轉(zhuǎn)義值。十六進(jìn)制轉(zhuǎn)義值必須為確定的兩個(gè)數(shù)字長。例如,'"x41' 匹配 "A"。'"x041' 則等價(jià)于 '"x04' & "1"。正則表達(dá)式中可以使用 ASCII 編碼。.
    "num 匹配 num,其中 num 是一個(gè)正整數(shù)。對(duì)所獲取的匹配的引用。例如,'(.)"1' 匹配兩個(gè)連續(xù)的相同字符。
    "n 標(biāo)識(shí)一個(gè)八進(jìn)制轉(zhuǎn)義值或一個(gè)向后引用。如果 "n 之前至少 n 個(gè)獲取的子表達(dá)式,則 n 為向后引用。否則,如果 n 為八進(jìn)制數(shù)字 (0-7),則 n 為一個(gè)八進(jìn)制轉(zhuǎn)義值。
    "nm 標(biāo)識(shí)一個(gè)八進(jìn)制轉(zhuǎn)義值或一個(gè)向后引用。如果 "nm 之前至少有 nm 個(gè)獲得子表達(dá)式,則 nm 為向后引用。如果 "nm 之前至少有 n 個(gè)獲取,則 n 為一個(gè)后跟文字 m 的向后引用。如果前面的條件都不滿足,若 n 和 m 均為八進(jìn)制數(shù)字 (0-7),則 "nm 將匹配八進(jìn)制轉(zhuǎn)義值 nm。
    "nml 如果 n 為八進(jìn)制數(shù)字 (0-3),且 m 和 l 均為八進(jìn)制數(shù)字 (0-7),則匹配八進(jìn)制轉(zhuǎn)義值 nml。
    "un 匹配 n,其中 n 是一個(gè)用四個(gè)十六進(jìn)制數(shù)字表示的 Unicode 字符。例如, "u00A9 匹配版權(quán)符號(hào) (?)。

    6. 部分例子

    正則表達(dá)式 說明
    /"b([a-z]+) "1"b/gi 一個(gè)單詞連續(xù)出現(xiàn)的位置
    /("w+):"/"/([^/:]+)(:"d*)?([^# ]*)/ 將一個(gè)URL解析為協(xié)議、域、端口及相對(duì)路徑
    /^(?:Chapter|Section) [1-9][0-9]{0,1}$/ 定位章節(jié)的位置
    /[-a-z]/ A至z共26個(gè)字母再加一個(gè)-號(hào)。
    /ter"b/ 可匹配chapter,而不能terminal
    /"Bapt/ 可匹配chapter,而不能aptitude
    /Windows(?=95 |98 |NT )/ 可匹配Windows95或Windows98或WindowsNT,當(dāng)找到一個(gè)匹配后,從Windows后面開始進(jìn)行下一次的檢索匹配。

    7. 正則表達(dá)式匹配規(guī)則

    7.1 基本模式匹配

    一切從最基本的開始。模式,是正規(guī)表達(dá)式最基本的元素,它們是一組描述字符串特征的字符。模式可以很簡單,由普通的字符串組成,也可以非常復(fù)雜,往往用特殊的字符表示一個(gè)范圍內(nèi)的字符、重復(fù)出現(xiàn),或表示上下文。例如:

    ^once

      這個(gè)模式包含一個(gè)特殊的字符^,表示該模式只匹配那些以once開頭的字符串。例如該模式與字符串"once upon a time"匹配,與"There once was a man from NewYork"不匹配。正如如^符號(hào)表示開頭一樣,$符號(hào)用來匹配那些以給定模式結(jié)尾的字符串。

    bucket$

      這個(gè)模式與"Who kept all of this cash in a bucket"匹配,與"buckets"不匹配。字符^和$同時(shí)使用時(shí),表示精確匹配(字符串與模式一樣)。例如:

    ^bucket$

      只匹配字符串"bucket"。如果一個(gè)模式不包括^和$,那么它與任何包含該模式的字符串匹配。例如:模式

    once

    與字符串

    There once was a man from NewYork
    Who kept all of his cash in a bucket.

    是匹配的。

    在該模式中的字母(o-n-c-e)是字面的字符,也就是說,他們表示該字母本身,數(shù)字也是一樣的。其他一些稍微復(fù)雜的字符,如標(biāo)點(diǎn)符號(hào)和白字符(空格、制表符等),要用到轉(zhuǎn)義序列。所有的轉(zhuǎn)義序列都用反斜杠(")打頭。制表符的轉(zhuǎn)義序列是:"t。所以如果我們要檢測(cè)一個(gè)字符串是否以制表符開頭,可以用這個(gè)模式:

    ^"t

    類似的,用"n表示“新行”,"r表示回車。其他的特殊符號(hào),可以用在前面加上反斜杠,如反斜杠本身用""表示,句號(hào).用".表示,以此類推。

    7.2 字符簇

    在INTERNET的程序中,正規(guī)表達(dá)式通常用來驗(yàn)證用戶的輸入。當(dāng)用戶提交一個(gè)FORM以后,要判斷輸入的電話號(hào)碼、地址、EMAIL地址、信用卡號(hào)碼等是否有效,用普通的基于字面的字符是不夠的。

    所以要用一種更自由的描述我們要的模式的辦法,它就是字符簇。要建立一個(gè)表示所有元音字符的字符簇,就把所有的元音字符放在一個(gè)方括號(hào)里:

    [AaEeIiOoUu]

    這個(gè)模式與任何元音字符匹配,但只能表示一個(gè)字符。用連字號(hào)可以表示一個(gè)字符的范圍,如:

    [a-z] //匹配所有的小寫字母
    [A-Z] //匹配所有的大寫字母
    [a-zA-Z] //匹配所有的字母
    [0-9] //匹配所有的數(shù)字
    [0-9"."-] //匹配所有的數(shù)字,句號(hào)和減號(hào)
    [ "f"r"t"n] //匹配所有的白字符

    同樣的,這些也只表示一個(gè)字符,這是一個(gè)非常重要的。如果要匹配一個(gè)由一個(gè)小寫字母和一位數(shù)字組成的字符串,比如"z2"、"t6"或"g7",但不是"ab2"、"r2d3" 或"b52"的話,用這個(gè)模式:

    ^[a-z][0-9]$

    盡管[a-z]代表26個(gè)字母的范圍,但在這里它只能與第一個(gè)字符是小寫字母的字符串匹配。

    前面曾經(jīng)提到^表示字符串的開頭,但它還有另外一個(gè)含義。當(dāng)在一組方括號(hào)里使用^是,它表示“非”或“排除”的意思,常常用來剔除某個(gè)字符。還用前面的例子,我們要求第一個(gè)字符不能是數(shù)字:

    ^[^0-9][0-9]$

    這個(gè)模式與"&5"、"g7"及"-2"是匹配的,但與"12"、"66"是不匹配的。下面是幾個(gè)排除特定字符的例子:

    [^a-z] //除了小寫字母以外的所有字符
    [^"""/"^] //除了(")(/)(^)之外的所有字符
    [^"""'] //除了雙引號(hào)(")和單引號(hào)(')之外的所有字符

    特殊字符"." (點(diǎn),句號(hào))在正規(guī)表達(dá)式中用來表示除了“新行”之外的所有字符。所以模式"^.5$"與任何兩個(gè)字符的、以數(shù)字5結(jié)尾和以其他非“新行”字符開頭的字符串匹配。模式"."可以匹配任何字符串,除了空串和只包括一個(gè)“新行”的字符串。

    PHP的正規(guī)表達(dá)式有一些內(nèi)置的通用字符簇,列表如下:

    字符簇 含義
    [[:alpha:]] 任何字母
    [[:digit:]] 任何數(shù)字
    [[:alnum:]] 任何字母和數(shù)字
    [[:space:]] 任何白字符
    [[:upper:]] 任何大寫字母
    [[:lower:]] 任何小寫字母
    [[:punct:]] 任何標(biāo)點(diǎn)符號(hào)
    [[:xdigit:]] 任何16進(jìn)制的數(shù)字,相當(dāng)于[0-9a-fA-F]

    7.3 確定重復(fù)出現(xiàn)

    到現(xiàn)在為止,你已經(jīng)知道如何去匹配一個(gè)字母或數(shù)字,但更多的情況下,可能要匹配一個(gè)單詞或一組數(shù)字。一個(gè)單詞有若干個(gè)字母組成,一組數(shù)字有若干個(gè)單數(shù)組成。跟在字符或字符簇后面的花括號(hào)({})用來確定前面的內(nèi)容的重復(fù)出現(xiàn)的次數(shù)。

    字符簇 含義
    ^[a-zA-Z_]$ 所有的字母和下劃線
    ^[[:alpha:]]{3}$ 所有的3個(gè)字母的單詞
    ^a$ 字母a
    ^a{4}$ aaaa
    ^a{2,4}$ aa,aaa或aaaa
    ^a{1,3}$ a,aa或aaa
    ^a{2,}$ 包含多于兩個(gè)a的字符串
    ^a{2,} 如:aardvark和aaab,但apple不行
    a{2,} 如:baad和aaa,但Nantucket不行
    "t{2} 兩個(gè)制表符
    .{2} 所有的兩個(gè)字符

    這些例子描述了花括號(hào)的三種不同的用法。一個(gè)數(shù)字,{x}的意思是“前面的字符或字符簇只出現(xiàn)x次”;一個(gè)數(shù)字加逗號(hào),{x,}的意思是“前面的內(nèi)容出現(xiàn)x或更多的次數(shù)”;兩個(gè)用逗號(hào)分隔的數(shù)字,{x,y}表示“前面的內(nèi)容至少出現(xiàn)x次,但不超過y次”。我們可以把模式擴(kuò)展到更多的單詞或數(shù)字:

    ^[a-zA-Z0-9_]{1,}$ //所有包含一個(gè)以上的字母、數(shù)字或下劃線的字符串
    ^[0-9]{1,}$ //所有的正數(shù)
    ^"-{0,1}[0-9]{1,}$ //所有的整數(shù)
    ^"-{0,1}[0-9]{0,}".{0,1}[0-9]{0,}$ //所有的小數(shù)

    最后一個(gè)例子不太好理解,是嗎?這么看吧:與所有以一個(gè)可選的負(fù)號(hào)("-{0,1})開頭(^)、跟著0個(gè)或更多的數(shù)字([0-9]{0,})、和一個(gè)可選的小數(shù)點(diǎn)(".{0,1})再跟上0個(gè)或多個(gè)數(shù)字([0-9]{0,}),并且沒有其他任何東西($)。下面你將知道能夠使用的更為簡單的方法。

    特殊字符"?"與{0,1}是相等的,它們都代表著:“0個(gè)或1個(gè)前面的內(nèi)容”或“前面的內(nèi)容是可選的”。所以剛才的例子可以簡化為:

    ^"-?[0-9]{0,}".?[0-9]{0,}$

    特殊字符"*"與{0,}是相等的,它們都代表著“0個(gè)或多個(gè)前面的內(nèi)容”。最后,字符"+"與 {1,}是相等的,表示“1個(gè)或多個(gè)前面的內(nèi)容”,所以上面的4個(gè)例子可以寫成:

    ^[a-zA-Z0-9_]+$ //所有包含一個(gè)以上的字母、數(shù)字或下劃線的字符串
    ^[0-9]+$ //所有的正數(shù)
    ^"-?[0-9]+$ //所有的整數(shù)
    ^"-?[0-9]*".?[0-9]*$ //所有的小數(shù)

    當(dāng)然這并不能從技術(shù)上降低正規(guī)表達(dá)式的復(fù)雜性,但可以使它們更容易閱讀。

    參考文獻(xiàn):
    JScript 和 VBScript 正則表達(dá)式

    微軟MSDN上的例子(英文):

    Scanning for HREFS
    Provides an example that searches an input string and prints out all the href="..." values and their locations in the string.
    Changing Date Formats
    Provides an example that replaces dates of the form mm/dd/yy with dates of the form dd-mm-yy.
    Extracting URL Information
    Provides an example that extracts a protocol and port number from a string containing a URL. For example, "http://www.contoso.com:8080/letters/readme.html" returns "http:8080".
    Cleaning an Input String
    provides an example that strips invalid non-alphanumeric characters from a string.
    Confirming Valid E-Mail Format
    Provides an example that you can use to verify that a string is in valid e-mail format

    posted @ 2008-11-24 14:32 lqx 閱讀(150) | 評(píng)論 (0)編輯 收藏

    JavaScript中的正則表達(dá)式

    正則表達(dá)式(regular expression)對(duì)象包含一個(gè)正則表達(dá)式模式(pattern)。它具有用正則表達(dá)式模式去匹配或代替一個(gè)串(string)中特定字符(或字符集合)的屬性(properties)和方法(methods)。 要為一個(gè)單獨(dú)的正則表達(dá)式添加屬性,可以使用正則表達(dá)式構(gòu)造函數(shù)(constructor function),無論何時(shí)被調(diào)用的預(yù)設(shè)置的正則表達(dá)式擁有靜態(tài)的屬性(the predefined RegExp object has static properties that are set whenever any regular expression is used, 我不知道我翻得對(duì)不對(duì),將原文列出,請(qǐng)自行翻譯)。
    • 創(chuàng)建:
      一個(gè)文本格式或正則表達(dá)式構(gòu)造函數(shù)
      文本格式: /pattern/flags
      正則表達(dá)式構(gòu)造函數(shù): new RegExp("pattern"[,"flags"]);
    • 參數(shù)說明:
      pattern -- 一個(gè)正則表達(dá)式文本
      flags -- 如果存在,將是以下值:
      g: 全局匹配
      i: 忽略大小寫
      gi: 以上組合

    [注意] 文本格式的參數(shù)不用引號(hào),而在用構(gòu)造函數(shù)時(shí)的參數(shù)需要引號(hào)。如:/ab+c/i new RegExp("ab+c","i")是實(shí)現(xiàn)一樣的功能。在構(gòu)造函數(shù)中,一些特殊字符需要進(jìn)行轉(zhuǎn)意(在特殊字符前加""")。如:re = new RegExp("""w+")

    正則表達(dá)式中的特殊字符

    字符 含意
    "

    做為轉(zhuǎn)意,即通常在"""后面的字符不按原來意義解釋,如/b/匹配字符"b",當(dāng)b前面加了反斜桿后/"b/,轉(zhuǎn)意為匹配一個(gè)單詞的邊界。
    -或-
    對(duì)正則表達(dá)式功能字符的還原,如"*"匹配它前面元字符0次或多次,/a*/將匹配a,aa,aaa,加了"""后,/a"*/將只匹配"a*"。

    ^ 匹配一個(gè)輸入或一行的開頭,/^a/匹配"an A",而不匹配"An a"
    $ 匹配一個(gè)輸入或一行的結(jié)尾,/a$/匹配"An a",而不匹配"an A"
    * 匹配前面元字符0次或多次,/ba*/將匹配b,ba,baa,baaa
    + 匹配前面元字符1次或多次,/ba*/將匹配ba,baa,baaa
    ? 匹配前面元字符0次或1次,/ba*/將匹配b,ba
    (x) 匹配x保存x在名為$1...$9的變量中
    x|y 匹配x或y
    {n} 精確匹配n次
    {n,} 匹配n次以上
    {n,m} 匹配n-m次
    [xyz] 字符集(character set),匹配這個(gè)集合中的任一一個(gè)字符(或元字符)
    [^xyz] 不匹配這個(gè)集合中的任何一個(gè)字符
    ["b] 匹配一個(gè)退格符
    "b 匹配一個(gè)單詞的邊界
    "B 匹配一個(gè)單詞的非邊界
    "cX 這兒,X是一個(gè)控制符,/"cM/匹配Ctrl-M
    "d 匹配一個(gè)字?jǐn)?shù)字符,/"d/ = /[0-9]/
    "D 匹配一個(gè)非字?jǐn)?shù)字符,/"D/ = /[^0-9]/
    "n 匹配一個(gè)換行符
    "r 匹配一個(gè)回車符
    "s 匹配一個(gè)空白字符,包括"n,"r,"f,"t,"v等
    "S 匹配一個(gè)非空白字符,等于/[^"n"f"r"t"v]/
    "t 匹配一個(gè)制表符
    "v 匹配一個(gè)重直制表符
    "w 匹配一個(gè)可以組成單詞的字符(alphanumeric,這是我的意譯,含數(shù)字),包括下劃線,如["w]匹配"$5.98"中的5,等于[a-zA-Z0-9]
    "W 匹配一個(gè)不可以組成單詞的字符,如["W]匹配"$5.98"中的$,等于[^a-zA-Z0-9]。

    說了這么多了,我們來看一些正則表達(dá)式的實(shí)際應(yīng)用的例子:

    E-mail地址驗(yàn)證:
     function test_email(strEmail) {
      var myReg = /^[_a-z0-9]+@([_a-z0-9]+".)+[a-z0-9]{2,3}$/;
      if(myReg.test(strEmail)) return true;
      return false;
     }
    HTML代碼的屏蔽
     function mask_HTMLCode(strInput) {
       var myReg = /<("w+)>/;
       return strInput.replace(myReg, "&lt;$1&gt;");
     }


    正則表達(dá)式對(duì)象的屬性及方法
    預(yù)定義的正則表達(dá)式擁有有以下靜態(tài)屬性:input, multiline, lastMatch, lastParen, leftContext, rightContext和$1到$9。其中input和multiline可以預(yù)設(shè)置。其他屬性的值在執(zhí)行過exec或test方法后被根據(jù)不同條件賦以不同的值。許多屬性同時(shí)擁有長和短(perl風(fēng)格)的兩個(gè)名字,并且,這兩個(gè)名字指向同一個(gè)值。(JavaScript模擬perl的正則表達(dá)式)
    正則表達(dá)式對(duì)象的屬性
    屬性 含義
    $1...$9 如果它(們)存在,是匹配到的子串
    $_ 參見input
    $* 參見multiline
    $& 參見lastMatch
    $+ 參見lastParen
    $` 參見leftContext
    $'          參見rightContext
    constructor    創(chuàng)建一個(gè)對(duì)象的一個(gè)特殊的函數(shù)原型
    global       是否在整個(gè)串中匹配(bool型)
    ignoreCase     匹配時(shí)是否忽略大小寫(bool型)
    input        被匹配的串
    lastIndex     最后一次匹配的索引
    lastParen     最后一個(gè)括號(hào)括起來的子串
    leftContext    最近一次匹配以左的子串
    multiline     是否進(jìn)行多行匹配(bool型)
    prototype     允許附加屬性給對(duì)象
    rightContext    最近一次匹配以右的子串
    source       正則表達(dá)式模式
    lastIndex     最后一次匹配的索引

    正則表達(dá)式對(duì)象的方法
    方法 含義
    compile      正則表達(dá)式比較
    exec        執(zhí)行查找
    test        進(jìn)行匹配
    toSource      返回特定對(duì)象的定義(literal representing),其值可用來創(chuàng)建一個(gè)新的對(duì)象。重載Object.toSource方法得到的。
    toString      返回特定對(duì)象的串。重載Object.toString方法得到的。
    valueOf      返回特定對(duì)象的原始值。重載Object.valueOf方法得到
    例子
    <script language = "JavaScript">
    var myReg = /(w+)s(w+)/;
    var str  = "John Smith";
    var newstr = str.replace(myReg, "$2, $1");
    document.write(newstr);
    </script>
    將輸出"Smith, John"


    〓簡介〓
    字符意義:對(duì)于字符,通常表示按字面意義,指出接著的字符為特殊字符,不作解釋。
    例如:/b/匹配字符'b',通過在b 前面加一個(gè)反斜杠,也就是/b/,則該字符變成特殊字符,表示匹配一個(gè)單詞的分界線。或者:對(duì)于幾個(gè)字符,通常說明是特殊的,指出緊接著的字符不是特殊的,而應(yīng)該按字面解釋。例如:*是一個(gè)特殊字符,匹配任意個(gè)字符(包括0個(gè)字符);例如:/a*/意味匹配0個(gè)或多個(gè)a。為了匹配字面上的*,在a前面加一個(gè)反斜杠;例如:/a*/匹配'a*'。

    〓正文〓
    字符
    意義:對(duì)于字符,通常表示按字面意義,指出接著的字符為特殊字符,不作解釋。
    例如:/b/匹配字符'b',通過在b 前面加一個(gè)反斜杠,也就是/b/,則該字符變成特殊字符,表示
    匹配一個(gè)單詞的分界線。
    或者:
    對(duì)于幾個(gè)字符,通常說明是特殊的,指出緊接著的字符不是特殊的,而應(yīng)該按字面解釋。
    例如:*是一個(gè)特殊字符,匹配任意個(gè)字符(包括0個(gè)字符);例如:/a*/意味匹配0個(gè)或多個(gè)a。
    為了匹配字面上的*,在a前面加一個(gè)反斜杠;例如:/a*/匹配'a*'。

    字符^
    意義:表示匹配的字符必須在最前邊。
    例如:/^A/不匹配"an A,"中的'A',但匹配"An A."中最前面的'A'。

    字符$
    意義:與^類似,匹配最末的字符。
    例如:/t$/不匹配"eater"中的't',但匹配"eat"中的't'。

    字符*
    意義:匹配*前面的字符0次或n次。
    例如:/bo*/匹配"A ghost booooed"中的'boooo'或"A bird warbled"中的'b',但不匹配"A goat g
    runted"中的任何字符。

    字符+
    意義:匹配+號(hào)前面的字符1次或n次。等價(jià)于{1,}。
    例如:/a+/匹配"candy"中的'a'和"caaaaaaandy."中的所有'a'。

    字符?
    意義:匹配?前面的字符0次或1次。
    例如:/e?le?/匹配"angel"中的'el'和"angle."中的'le'。

    字符.
    意義:(小數(shù)點(diǎn))匹配除換行符外的所有單個(gè)的字符。
    例如:/.n/匹配"nay, an apple is on the tree"中的'an'和'on',但不匹配'nay'。


    字符(x)
    意義:匹配'x'并記錄匹配的值。
    例如:/(foo)/匹配和記錄"foo bar."中的'foo'。匹配子串能被結(jié)果數(shù)組中的素[1], ..., [n] 返
    回,或被RegExp對(duì)象的屬性$1, ..., $9返回。

    字符x|y
    意義:匹配'x'或者'y'。
    例如:/green|red/匹配"green apple"中的'green'和"red apple."中的'red'。

    字符{n}
    意義:這里的n是一個(gè)正整數(shù)。匹配前面的n個(gè)字符。
    例如:/a{2}/不匹配"candy,"中的'a',但匹配"caandy," 中的所有'a'和"caaandy."中前面的兩個(gè)
    'a'。

    字符{n,}
    意義:這里的n是一個(gè)正整數(shù)。匹配至少n個(gè)前面的字符。
    例如:/a{2,}不匹配"candy"中的'a',但匹配"caandy"中的所有'a'和"caaaaaaandy."中的所有'a'

    字符{n,m}
    意義:這里的n和m都是正整數(shù)。匹配至少n個(gè)最多m個(gè)前面的字符。
    例如:/a{1,3}/不匹配"cndy"中的任何字符,但匹配 "candy,"中的'a',"caandy," 中的前面兩個(gè)
    'a'和"caaaaaaandy"中前面的三個(gè)'a',注意:即使"caaaaaaandy" 中有很多個(gè)'a',但只匹配前面的三
    個(gè)'a'即"aaa"。

    字符[xyz]
    意義:一字符列表,匹配列出中的任一字符。你可以通過連字符-指出一個(gè)字符范圍。
    例如:[abcd]跟[a-c]一樣。它們匹配"brisket"中的'b'和"ache"中的'c'。

    字符[^xyz]
    意義:一字符補(bǔ)集,也就是說,它匹配除了列出的字符外的所有東西。 你可以使用連字符-指出一
    字符范圍。
    例如:[^abc]和[^a-c]等價(jià),它們最早匹配"brisket"中的'r'和"chop."中的'h'。

    字符[b]
    意義:匹配一個(gè)空格(不要與b混淆)

    字符b
    意義:匹配一個(gè)單詞的分界線,比如一個(gè)空格(不要與[b]混淆)
    例如:/bnw/匹配"noonday"中的'no',/wyb/匹配"possibly yesterday."中的'ly'。

    字符B
    意義:匹配一個(gè)單詞的非分界線
    例如:/wBn/匹配"noonday"中的'on',/yBw/匹配"possibly yesterday."中的'ye'。

    字符cX
    意義:這里的X是一個(gè)控制字符。匹配一個(gè)字符串的控制字符。
    例如:/cM/匹配一個(gè)字符串中的control-M。

    字符d
    意義:匹配一個(gè)數(shù)字,等價(jià)于[0-9]。
    例如:/d/或/[0-9]/匹配"B2 is the suite number."中的'2'。

    字符D
    意義:匹配任何的非數(shù)字,等價(jià)于[^0-9]。
    例如:/D/或/[^0-9]/匹配"B2 is the suite number."中的'B'。

    字符f
    意義:匹配一個(gè)表單符

    字符n
    意義:匹配一個(gè)換行符

    字符r
    意義:匹配一個(gè)回車符

    字符s
    意義:匹配一個(gè)單個(gè)white空格符,包括空格,tab,form feed,換行符,等價(jià)于[ fnrtv]。
    例如:/sw*/匹配"foo bar."中的' bar'。

    字符S
    意義:匹配除white空格符以外的一個(gè)單個(gè)的字符,等價(jià)于[^ fnrtv]。
    例如:/S/w*匹配"foo bar."中的'foo'。

    字符t
    意義:匹配一個(gè)制表符

    字符v
    意義:匹配一個(gè)頂頭制表符

    字符w
    意義:匹配所有的數(shù)字和字母以及下劃線,等價(jià)于[A-Za-z0-9_]。
    例如:/w/匹配"apple,"中的'a',"$5.28,"中的'5'和"3D."中的'3'。

    字符W
    意義:匹配除數(shù)字、字母外及下劃線外的其它字符,等價(jià)于[^A-Za-z0-9_]。
    例如:/W/或者/[^$A-Za-z0-9_]/匹配"50%."中的'%'。

    字符n
    意義:這里的n是一個(gè)正整數(shù)。匹配一個(gè)正則表達(dá)式的最后一個(gè)子串的n的值(計(jì)數(shù)左圓括號(hào))。

    例如:/apple(,)sorange1/匹配"apple, orange, cherry, peach."中的'apple, orange',下面
    有一個(gè)更加完整的例子。
    注意:如果左圓括號(hào)中的數(shù)字比n指定的數(shù)字還小,則n取下一行的八進(jìn)制escape作為描述。

    字符ooctal和xhex
    意義:這里的ooctal是一個(gè)八進(jìn)制的escape值,而xhex是一個(gè)十六進(jìn)制的escape值,允許在一個(gè)正則表達(dá)式中嵌入ASCII碼。

    posted @ 2008-11-24 14:31 lqx 閱讀(226) | 評(píng)論 (0)編輯 收藏

    主站蜘蛛池模板: 久久aⅴ免费观看| 抽搐一进一出gif免费视频| www亚洲精品久久久乳| 美女被爆羞羞网站免费| 亚欧国产一级在线免费| a级毛片在线视频免费观看| 中文字幕在线免费观看| 性一交一乱一视频免费看| 亚洲国产一级在线观看 | 青青青国产色视频在线观看国产亚洲欧洲国产综合 | 国产亚洲美女精品久久久| 亚洲AV成人一区二区三区AV| 亚洲日韩国产精品无码av| 亚洲免费网站观看视频| 一级毛片a女人刺激视频免费| 今天免费中文字幕视频| 青青视频观看免费99| 免费a在线观看播放| 亚洲国产精品自在在线观看| 91丁香亚洲综合社区| 亚洲免费视频一区二区三区| 99久热只有精品视频免费看| 免费观看a级毛片| 亚洲人成网站在线播放vr| 亚洲电影在线播放| 国产尤物在线视精品在亚洲| 国产精品偷伦视频观看免费| 免费a级毛片高清视频不卡 | 亚洲国产精华液网站w| 亚洲精品亚洲人成在线播放| eeuss影院ss奇兵免费com| 性xxxxx免费视频播放| 久久久久国产成人精品亚洲午夜| 亚洲综合在线成人一区| 免费国产高清毛不卡片基地| 91久久精品国产免费一区| 亚洲AV无码一区二三区| 亚洲一卡二卡三卡| 中文永久免费观看网站| 成人免费视频试看120秒| 亚洲成AV人片在线观看WWW|