In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to escape in the rule, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.
1 Overview
This may be a confusing, even confusing topic, but it is precisely because of this that there is a need for discussion.
In the rule, some characters with special meaning, or character sequences, are called metacharacters, such as "?" Indicates that the modified subexpression matches 0 or 1 times, "(? I)" means to ignore case matching patterns, and so on. When these metacharacters are required to match themselves, they are escaped.
In different languages or application scenarios, the way of regular definition, the location of metacharacters are different, and the ways of escape are numerous and different.
2. Character escape in .NET rules
2.1 escape characters in .NET rules
In most languages, "\" is used as an escape character to escape characters or sequences of characters with special meaning, such as "\ n" for line breaks, "\ t" for horizontal tabs, and so on. And when such an escape is applied to the regularity, there will be some unexpected changes.
The topic is drawn from a regular question in C#.
The copy code is as follows:
String [] test = new string [] {"\", "\\"}
Regex reg = new Regex ("^\\ $")
Foreach (string s in test)
{
RichTextBox2.Text + = "Source string:" + s.PadRight (5,') + "match result:" + reg.IsMatch (s) + "\ n"
}
/ *-output-
Source string:\ match result: True
Source string:\\ match result: False
, /
Some people may be confused about this result. Doesn't the "\" in the string represent an escaped "\" character? And shouldn't "\" represent two escaped "\" characters? So the result of the above regular match should be the first one is False and the second is True?
For this problem, the direct explanation may not be easy to understand, let's explain it in a different way.
For example, the characters to be matched are like this.
String test = "("
So how to write the rules? Because "(" has a special meaning in the rule, it must be escaped when writing the rule, that is, "\", while in the string, "\" is used to represent the "\" itself, that is,
Regex reg = new Regex ("^\ ($")
If you understand this, then change the "(" back to "\"). By the same token, in a string, use "\" to represent "\" itself, that is,
Regex reg = new Regex ("^\\ $")
Through this analysis, we can see that in fact, in a regular declaration in the form of a string, "\" matches a single "\" character. Summarize the relationship between them:
The string output to the console or interface:\
The string declared in the program: string test = "\\"
The rule declared in the program: Regex reg = new Regex ("^\\ $")
Is this explanation understandable, so does it feel clumsy? Yes, a regular declared as a string in a program is so clumsy when it comes to escapes.
So in C#, there is another way to declare a string. By adding an "@" to the string, you can ignore the escape.
The copy code is as follows:
String [] test = new string [] {@ "\", @ "\"}
Regex reg = new Regex (@ "^\ $")
Foreach (string s in test)
{
RichTextBox2.Text + = "Source string:" + s.PadRight (5,') + "match result:" + reg.IsMatch (s) + "\ n"
}
/ *-output-
Source string:\ match result: True
Source string:\\ match result: False
, /
This makes it much more concise and conforms to the usual understanding.
But at the same time, it also brings another problem, that is, the escape of double quotes. In a normal string declaration, double quotation marks can be escaped with "\".
String test = "only a test"
However, after the string is preceded by "@", "\" is recognized as the "\" character itself, so that double quotes cannot be escaped with "\". Double quotes need to be escaped with ".
String test = @ "only a test"
In VB.NET, there is only one form of regular definition, which is consistent with the definition of "@" in C#.
The copy code is as follows:
Dim test As String () = New String () {"\", "\"}
Dim reg As Regex = New Regex ("^\ $")
For Each s As String In test
RichTextBox2.Text + = "Source string:" & s.PadRight (5, "" c) & "matching result: & reg.IsMatch (s) & vbCrLf
Next
'- output-
'Source string:\ match result: True
'Source string:\\ match result: False
'-
2.2 Metacharacters that need to be escaped in .NET rules
In MSDN, the following characters, as metacharacters in the rule, need to be escaped when matching themselves
. $^ {[(|) * +?\
However, in practical application, according to the actual situation, the above characters may not need to be escaped, or more than the above characters may need to be escaped.
In the normal process of regular writing, the escape of the above characters can usually be handled by the writer normally, but you need to pay special attention when generating regularities dynamically, otherwise, when the variables contain metacharacters, dynamically generated regularities may throw exceptions at compile time. Fortunately, the Regex.Escape method is provided in .NET to deal with this problem. For example, according to the dynamically acquired id to extract the corresponding div tag content.
String id = Regex.Escape (textBox1.Text)
Regex reg = new Regex (@ "(? is))
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.