How to use XSL and regular expressions to verify the validity of data (i)

Source: Internet
Author: User
Tags contains expression regular expression valid xsl
Data | Regular Series 17: How to use XSL and regular expressions to verify the validity of data
XSL is now gradually becoming a similar and SQL in XML in the database design position.
Although Microsoft's XSL is simply a function of some of these parts
But you've been able to implement very complex queries.
Although today's XSL is just a form of pure text and string
The query Language
In the example described below, a search that uses a large number of strings in the text,
You will find that a large part of the processing of data in XML is to use the query in the text.
This is a very popular feature in the writing of XSL.
Because of this, you should understand how some regular expressions should be used.

A brief introduction to regular expressions
Regular expressions are mostly from the needs of the UNIX world.
In Unix, many programming languages are almost entirely centered around regular expressions (Perl, Python, TCL, for example).
But it's strange that regular expressions seem to have been used recently in the Windows family,
Especially in scripting languages, such as JavaScript and VBScript,
Although you can also use them in Visual Basic or Java, it is clear that they appear to be
More appealing in scripting languages. Perhaps for this reason, it seems that people rarely use regular expressions.

Using regular expressions, you can create a matching template based on what you want to query (English is called pattern).
Once you have used a regular expression to create a template, you can use it to test your string.
Use it to accomplish a number of functions:
For example, to determine whether a string is in another string (or where in another string)
For example, use a string to replace another string
For example, returns a list of all strings that meet template criteria
For example... Wait, wait.

I've covered the basic concepts of regular expressions, with detailed descriptions and syntax for MSDN and
The Help in JavaScript.
In VB, if you want to refer to regular expressions, you need to refer to "Microsoft VBScript Regular Expressions" in your project. But if you use a script you don't have to, because in the script it's already a
The inner object is for you to quote.
Of course you need to install IE4 on your machine.
This object (in JavaScript) is called RegExp
Here's the code to illustrate the problem, now suppose you want to see if a text block contains a specific
Strings (for example, "regular Expressions")
Code See below:
The code is written using VB.
Public Function istermindocument (FilePath as String,_
Expr as String) as Boolean
Dim FS as FileSystemObject
Dim TS as TextStream
Dim Re as RegExp
Dim text as String

Set fs = New FileSystemObject
Set ts = fs. OpenTextFile (FilePath, ForReading)
Text = ts. ReadAll
Set re = New RegExp
Re. pattern = Expr
Istermindocument = Re. Test (text)
End Function

Debug.Print Istermindocument ("C:\bin\myPage.htm", _
"Regular expression")
The above function returns TRUE/FALSE according to whether there is a string in the document that satisfies the condition.
Pay attention to my bold section:
The first sentence is an object that establishes a regular expression
The second sentence is the template that specifies the regular expression
The third sentence is based on the template to execute the query
Oh, if the function of regular expression is just so simple, maybe you will say
VB in the InStr () can not replace it?

However, when XML data is formatted, the processing of strings is far more complex than this.
For example: Suppose you want to make sure that the field you want to verify contains a well-formed ZIP code
(well-formed means that it's a valid encoding,
Maybe it's not valid for a given place or region or country.
This type of well-formed and valid expression will be the focus of the discussion in this article.
If you use VB to make this kind of judgment will be very ugly
You need to decide whether the expression has 5 digits or 10 digits, or whether it's a letter,
And then the 6th digit must be a broken number.
But if you are using regular expressions, it will be as simple as this:

Set Iszipcode = New RegExp
Iszipcode.pattern = "^\d{5}" (-\d{4})? $ "
If Iszipcode.test ("32545-2198") Then

Here's a quick explanation of what the template means:
^ Description There is no other string before this expression,
means that the expression to be validated is not part of a string, but rather its beginning
\d indicates that the next character must be a number in 0-9
\D{5} and must be a connected 5 digits
-\D{4} 4 digits must appear in the back of the character "-"
(-\d{4})? These 4 numbers are optional and can be either
$ there should be nothing else behind this expression.

The most interesting thing is that once you have defined such a template, you can use it in
Any other regular expression object, without having to re-establish a regular expression object.
Using this approach, you can even put a JavaScript program with nearly 2000 lines of code
Reduced to only hundreds of lines, set when you put together a number of templates, you can complete
Regular expression is not possible to complete the dongdong.

Let me cite another example to verify the validity of the data:
For example, you now want to verify that a phone number data is valid
For a general purpose a telephone number is generally valid for the following ways:
(800) # 555-1212
1 (800) 555-1212
1-800-555-1212
1.800.555.1212
Wait a minute.

If you use a script to write a piece of code that satisfies all of the requirements above, it will be very complicated.
But if you use regular expressions, it will be very simple, just the following two lines of code:
Set isphonenumber=new REGEXP
Isphonenumber.pattern= "^[01]?\s*[\ (\.-)?" ( \D{3}) [\) \.-]?\s* (\d{3}) [\.-] (\d{4}) $ "

You can understand the meaning of the above code carefully.
First it verifies whether the first character is 0 or 1 or not at all.
Then the following verification, we can think of the other part of the meaning of the, hehe.

Querying and replacing data
Of course, validating the validity of data is just a trivial thing that it can do,
But what's more useful is that if you can change the way a number of phone numbers are expressed
Show up in a unified way.
For example, I want to format the phone number above as a fragment in XML as follows:

<phoneNumber>
<areacode>123</areacode>
<exchange>456</exchange>
<local>7890</local>
</phoneNumber>

The template for the phone number is divided into three sections:
(\d{3}), (\d{3}), (\d{4}), representing area code, Exchange, and local number
. In regular expressions, the regular interpreter automatically
Match the character assignments value to the variable $, $, and so on.
So you can use the following code to achieve

Re. Replace ("1 (352) 351-4159", "<phonenumber><areacode>$1</areacode><exchange>$2</ Exchange><local>$3</local></phonenumber> ")

If you feel that this replace is not comfortable to use, the following will give a
Similar to the Replace in VB and extends its function

Public Function Replacex (sourcestr as String, oldstr as _
String, newstr As String, optional ignoreCase as _
Boolean = False,optional IsGlobal as Boolean = True)
Dim Re as New REGEXP
Re. Pattern = Oldstr
Re. Global = IsGlobal
Re. IgnoreCase = IgnoreCase
Replacex = Re. Replace (Sourcestr, NEWSTR)
End Function

Here are some examples of how to use it:
Debug.Print Replacex (' is a test ', ' is ', ' at ')
--> "at a test"
The most exciting thing is to use regular expressions.
Debug.Print Replacex (' is a test ', ' \ws ', ' at ')
--> "that at a



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.