Regular basis decimal point _ Regular expression

Source: Internet
Author: User
Some details
For most languages and tools that use a traditional NFA engine, such as Java,. NET, "." The matching range is to match any character except the newline character "\ n".
But it's a bit special for JavaScript, because the browser's parsing engine is different, "." The matching range is also different for Trident kernel browsers, ie, "." It also matches any character other than the newline character "\ n", but for other kernel browsers, such as Firefox, Opera, and Chrome, "." is to match any character except the carriage return "\ r" and the newline character "\ n".

Some guesses about this detail
Copy Code code as follows:

# <script type= "Text/javascript" >
# document.write (/./.test ("\ r") + "<br/>");
# document.write (/./.test ("\ n") + "<br/>");
# </script>
#//ie the output
# True
# false
#//firefox, Opera, chrome output
# false
# false

Presumably, Trident, Presto, and gecko should all be traditional NFA engines, and WebKit at least support the traditional NFA engine, but unlike the traditional NFA engine, it's not the traditional NFA engine for advanced optimization, it's the dfa/ NFA hybrid engine.
Because "\ r" and "\ n" are supported under Windows, and only "\ n" is supported under UNIX, I suspect that it is possible that the other browser engine does not come from Windows and therefore does not provide support for "\ R", resulting in a regular "." Nor does it match "\ r". Not doing in-depth research, just a few guesses.
Common Application Mistakes
Attention
When matching multiple rows, do not attempt to match any character with "[. N]", which means only one of the decimal and newline characters, which you can use "(. | \ n) ", but it is not used in this way, which is less readable, less efficient, generally" [\s\s] "or". " Add (? s) matching mode to achieve this effect.

examples
Requirements Description: Matching <td> labeling Content
SOURCE string: <td>this is a test line.
Another line. </td>
Match result: <td>this is a test line.
Another line. </td>
Regular expression a:<td>[\s\s]*</td>
Regular expression two: (? s) <td>.*</td>
Match Efficiency Test
The following is a string for the test, that is, the content entered in the richTextBox1.Text below (from the CSDN home page):
Copy Code code as follows:

<link href= "Images/favicon.ico" rel= "Shortcut ICON"/>
<title>csdn.net-China's leading IT technology community, providing the most comprehensive information dissemination and service platform for IT Professionals </title>
<script language= ' JavaScript ' type= ' text/javascript ' src= ' http://www.csdn.net/ggmm/csdn_ggmm.js ' script> <script type= "Text/javascript" src= "http://counter.csdn.net/a/js/areacounter.js%22%3e%3c/script>"
<script type= "Text/javascript" >

Test code:
Copy Code code as follows:

# string yourstr = richTextBox1.Text;
# StringBuilder src = new StringBuilder (4096);
# for (int i = 0; I < 10000 i++)
# {
# src. Append (YOURSTR);
#}
# string strdata = src. ToString ();
# list<regex> reg = new list<regex> ();
# Reg. ADD (New Regex (@ "[\s\s]"));
# Reg. ADD (New Regex (@ "[\w\w]"));
# Reg. ADD (New Regex (@ "[\d\d]"));
# Reg. ADD (The New Regex (. | \ n)));
# Reg. ADD (new Regex (s).));
# String test = string. Empty;
# Stopwatch STOPW = new stopwatch ();
# foreach (Regex re in reg)
# {
# stopw.reset ();
# Stopw.start ();
# test = strdata;
# test = Re. Replace (Test, "");
# stopw.stop ();
# Richtextbox2.text + = "Regular expression:" + Re. ToString (). PadRight (10) + "Execution time:" + stopW.ElapsedMilliseconds.ToString () + "MS";
# Richtextbox2.text + = \---------------------------------------\ n;
#}

Test results:
The test is performed in two groups, with a memory footprint of 921M before the program executes
A group of unused quantifiers is substituted for only one character at a time, the execution times are as follows, consuming memory 938M
Copy Code code as follows:

Regular expression: [\s\s] Execution time: 2651 ms
---------------------------------------
Regular expression: [\w\w] Execution time: 2515 ms
---------------------------------------
Regular expression: [\d\d] Execution time: 2187 ms
---------------------------------------
Regular expression: (. | \ n) Execution time: 2470 ms
---------------------------------------
Regular expression: (? s). Execution Time: 1969 MS

Another group uses quantifiers, replaces all characters at a time, executes as follows, takes up memory 1128M
Copy Code code as follows:

Test results (with quantifiers)
Regular expression: [\s\s]+ Execution time: 249 ms
---------------------------------------
Regular expression: [\w\w]+ Execution time: 348 ms
---------------------------------------
Regular expression: [\d\d]+ Execution Time: 198 ms
---------------------------------------
Regular expression: (. | \ n) + Execution time: 879 ms
---------------------------------------
Regular expression: (? s). + Execution Time: 113 ms
---------------------------------------

Test Results Analysis:
The highest matching efficiency is the use of the "." of the singleline pattern.
Next is "[\d\d]", and "(. | \ n) "has the lowest matching efficiency
The "[\s\s]" match efficiency is centered, but it's used more than usual.

Note: Due to the different engines supported by each language, even if the same engine is used, the tuning is different for regular optimizations, so the above performance test conclusions may apply only to. NET.

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.