XML preference series (9)

Source: Internet
Author: User
Tags string methods
Series 17: how to apply XSL and regular expressions to verify the effectiveness of data XSL is gradually becoming a role similar to SQL in database design in XML. Although Microsoft's XSL only implements some of these functions Series 17: how to apply XSL and regular expressions to verify data validity
XSL is gradually becoming a role in XML similar to SQL in database design.
Although Microsoft's XSL only implements some of the functions
However, you can implement very complex queries.
Although the current XSL is only a method based on pure text and string
Query Language
In the following example, we will apply a large number of string methods in text to search,
You will find that a large part of data processing in XML must be applied to queries in text.
This is a very popular function in the compilation of XSL.
Because of this, you should know how to apply some regular expressions.

A brief introduction to regular expressions
Most regular expressions come from the Unix world.
In Unix, almost all programming languages are filled with regular expressions (such as Perl, Python, and Tcl)
However, it is surprising that regular expressions seem to have recently been used in the Windows series,
In particular, a large number of applications are used in scripting languages, such as JavaScript and VBScript,
Although you can also apply them to Visual Basic or Java, it seems that
Scripting languages are more attractive. This may be the reason. generally, regular expressions are rarely applied.

Apply a regular expression. you can create a matching template (pattern) based on the content you want to query)
Once you apply a regular expression to create a template, you can apply it to test your string,
Using it can accomplish many functions:
For example, to determine whether a string is in another string (or what position is in another string)
For example, apply a string to replace another string.
For example, return the list of all strings meeting the template conditions.
For example... And so on.

In the above section, I first introduced the basic concepts of regular expressions. For more information about its clarification and syntax, see MSDN and
Help in JavaScript.
In VB, if you want to reference a Regular expression, you need to reference 'Microsoft VBScript Regular expressions' in the project '. However, it is unnecessary to apply the script because it is already
The internal object is for your reference.
Of course, you need to install IE4 or above on your machine.
This object (in JavaScript) is called RegExp
Let the Code clarify the title. Now suppose you want to check whether a text block contains a specific
String (for example, 'Regular expressions ')
For the code, see:
The code is written in 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 will return True/False based on whether there are strings that meet the conditions in the document.
Pay attention to the bold parts:
The first sentence is to create a regular expression object.
The second sentence is to specify the regular expression template.
The third statement is to perform the query according to the template.
If the function of a regular expression is only so simple, you may say
Can instr () in VB be replaced?

However, the processing of strings is far more complicated when XML data is structured.
For example, suppose you want to ensure that the field you want to verify contains a well-formed zip code.
(Well-formed means it is a valid code,
Maybe it is invalid for a given location or a region or country
This well-formed and valid expressions will be the focus of this article)
If you use VB for such determination, it will be very embarrassing.
You need to determine whether the expression has five or ten digits, or whether it is a letter,
Then the 6th-character letter must be a broken rule number.
However, if a regular expression is applied, it will be as simple as follows:

Set IsZipCode = New RegExp
IsZipCode. Pattern = '^ \ d {5} (-\ d {4 })? $'
If IsZipCode. test ('2017-32545 ') then

The following briefly describes the meaning of the template:
^ Clarify that there are no other strings before this expression,
It means that the expression to be verified is not a part of a string, but the beginning of it.
\ D indicates that the next character must be a number in 0-9.
\ D {5} and must be connected to 5 numbers
-\ D {4} four numbers must be displayed after the '-' character
(-\ D {4 })? The four numbers are optional.
$ This expression should be followed by nothing else.

The most interesting thing is that once you have defined such a template, you can apply it
In any other regular expression object, you do not need to recreate a regular expression object.
With this measure, you can even put a JavaScript program with nearly 2000 lines of code
Reduced to hundreds of rows. the setting can be completed when some templates are combined.
The regular expression is impossible.

Here is another example to verify the effectiveness of data occupation:
For example, if you want to verify whether a phone number is valid
For a general phone number, the following statements are valid:
(800) 555-1212
1 (800) 555-1212
1-800-555-1212
1.800.555.1212
And so on.

It would be very complicated to write a piece of code that satisfies all the preceding requests.
However, if you apply a regular expression, it will be very simple. there are only the following two codes:
Set IsPhoneNumber = new RegExp
IsPhoneNumber. pattern = '^ [01]? \ S * [\ (\.-]? (\ D {3}) [\) \.-]? \ S * (\ d {3}) [\.-] (\ d {4}) $'

You can carefully understand the meaning of the above code.
First, it verifies whether the first character is 0 or whether it is 1 or basically no.
Then we will perform the following verification. you can try to figure out what the other part means.

Query and exchange data
Of course, verifying the effectiveness of data is only a small task,
But what's more useful is: if you can convert the expressions of the above multiple phone numbers
Is displayed in the same way.
For example, I want to convert the above phone number format into a segment in XML as follows:


123
456
7890


The telephone number Template is divided into three parts:
(\ D {3}), (\ d {3}), (\ d {4}), distinguish between area code, exchange, and local number
. In a regular expression, the regular expression interpreter takes the initiative
Assign matching characters to variables $1, $2, $3, and so on.
In this way, you can implement the following code:

Re. Replace ('1 (352) 351-00009 ',' $1 $2 $3 ')

If you feel that this Replace application is uncomfortable, we will provide
Similar to Replace in VB and extended its function

Public Function Replacex (sourceStr as String, oldStr _
String, newStr as String, optional ignoreCase _
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

The following are some examples of its application:
Debug. Print Replacex ('This is a test', 'is ', 'AT ')
--> 'That at a test'
The best thing is to apply a regular expression.
Debug. Print Replacex ('This is a test', '\ Ws', 'AT ')
--> 'That at a tatt'
You can even
Debug. Print Replacex ('This is a test', '(\ ws)', 'At $1 ')
--> 'Thatis atis a tatist'

The replace method of the regular expression has two parameters.
In the default case, the regular expression stops when a condition is found.
However, if you set the isGlobal parameter to True, it will change the full text
By default, regular expressions are case-sensitive.

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.