VBS two ways to create regular expression objects _vbs

Source: Internet
Author: User
All along, I used the New RegExp to create regular expression objects:
Set regex = new RegExp However, it is also possible to create a regular expression object that is new to the VBS:
Set regex = CreateObject ("VBScript.RegExp") it seems that none of the VBScript books I've read has been written about this, and of course there's still a lot of stuff that's not on the books.

Now that a regular expression is mentioned, by the way, the regular expression object of VBS supports MultiLine mode, and the object has a MultiLine property. You can see the properties and methods of the COM object by running Visual C + + with its own OLE View, the left tree expanding Type libraries, and finding Microsoft VBScript Regular Expressions 5.5:
Copy Code code as follows:

Interface Iregexp2:idispatch {
[ID (0x00002711), propget]
HRESULT pattern ([out, retval] bstr* ppattern);
[ID (0x00002711), propput]
HRESULT pattern ([in] BSTR Ppattern);
[ID (0x00002712), propget]
HRESULT IgnoreCase ([out, retval] variant_bool* pignorecase);
[ID (0x00002712), propput]
HRESULT IgnoreCase ([in] Variant_bool pignorecase);
[ID (0x00002713), propget]
HRESULT Global ([out, retval] variant_bool* pglobal);
[ID (0x00002713), propput]
HRESULT Global ([in] Variant_bool Pglobal);
[ID (0x00002717), propget]
HRESULT Multiline ([out, retval] variant_bool* pmultiline);
[ID (0x00002717), propput]
HRESULT Multiline ([in] Variant_bool pmultiline);
[ID (0x00002714)]
HRESULT Execute (
[In] BSTR sourcestring,
[Out, retval] idispatch** ppmatches);
[ID (0x00002715)]
HRESULT Test (
[In] BSTR sourcestring,
[Out, retval] variant_bool* Pmatch);
[ID (0x00002716)]
HRESULT Replace (
[In] BSTR sourcestring,
[In] VARIANT Replacevar,
[Out, retval] bstr* pdeststring);
};

However, many books are not introduced, Vbsedit automatic completion of the function does not hint MultiLine attributes, even Microsoft's official reference manual is not.

Finally, for a simple example, if you want to delete a blank line in the text, you can use the following code:
Copy Code code as follows:

' Author:demon
' Link:http://demon.tw/programming/vbs-regexp-object.html
' Date:2011/12/26
Dim FSO, regex, str
Set fso = CreateObject ("Scripting.FileSystemObject")
Set regex = CreateObject ("VBScript.RegExp")
str = FSO. OpenTextFile ("Demon.txt"). ReadAll
Regex. Global = True
Regex. MultiLine = True
Regex. Pattern = "^\s*\n"
str = regex. Replace (str, "")
' The equivalent of str in javascript = str.replace (/^\s*\n/gm, "")
Fso. OpenTextFile ("Demon.txt", 2). Write Str

Original from: http://demon.tw/programming/vbs-regexp-object.html

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.