Where the containing string contains double quotation marks, then two double quotation marks, instead of a backslash plus double quotation marks (\ "), nor a slash plus double quotation mark (/")
The regular expression gets the image inside the CSS example, inside has the URL inside the picture address has the double quotation mark, must notice uses two double quotation marks "" The expression
static void Main ( String[] args) {regex reg = new Regex (@ "url\ (['" "]?) (. +[^ ' ""]) \1\) "); // Note the quotation marks in double quotes instead of the backslash Console.WriteLine (Reg. Match (@ "{background-image:url (//ssl.gstatic.com/ui /v1/menu/checkmark.png); Backgro// output URL (//ssl.gstatic.com/ui/v1/menu/checkmark.png) Console.readkey ();}
A back reference with a group name is \k<num> in C #, which matches the repeating word:
void Main (string[] args) { new Regex (@ "\b (? <group>\w+ +) \k<group>" "What's the hell is talking about?" ; Console.WriteLine (Reg. Match (str)); Console.readkey (); }
When you new a Regex object in C #, the second parameter can support the selection of matching patterns with enumerations, and now the effect of these enumeration values on the regular.
Mode description
. Singleline Dot can match any character
. Multiline extension ^ and $ match so that ^ and $ can match line breaks inside a string
. Ignorepatternwhitespace design loose arrangement and annotation mode
. IgnoreCase a case-insensitive match
. ECMAScript restricts \w \s \d, making it valid only for ASCII characters
. The driving process of the RightToLeft drive is constant, but in the opposite direction (starting at the end of the character and moving toward the beginning)
. Compiled spend more time optimizing regular expressions, compiling into DLLs, and consuming multiple points of memory, but matching is faster.
. Explicitcapture normal brackets () are normally captured in parentheses, but in this mode with (?: ...) Same, grouped, not captured
The meaning of regexoptions.compiled
Comparison of use of regexoptions.compiled with non-use of regexoptions.compiled
Standard does not use the use
Faster startup speed (up to 60 times times)
Less memory consumption (each regular expression consumes 5-15kb)
Up to 10 times times faster matching speed
When regexoptons.compiled is used, this memory is always occupied and cannot be released during program execution, so this option is only appropriate for regular expressions that are used frequently.
ECMAScript mode
Note that ECMAScript can only be used in conjunction with the following options
Regexoptons.ignorecase
Regexoptons.multiline
Regexoptons.compiled
And the backslash-number does not have the two semantics of a reverse reference and a decimal transfer, because it can only represent a reverse reference. For example, \10 represents a reverse reference \1 and then the literal 0. If this mode is not enabled, \12 matches the ASCII feed character linefeed. At the same time \w \d \s \w \d \s can only match ASCII.
Also in C #, the number of groupings needs to be noted.
Grouping 0 is the result of matching the entire regular expression.
Then, in turn, are unnamed groupings.
Finally, a named grouping.
For example:
(\w) (? <num>\d+) (\s+)
1 3 2
Special replacement treatment
Both the Regex.Replace method and the Match.result method can receive replacement strings that are capable of special processing. The following sequence of characters is replaced by the text that is matched to:
Character sequence replacement content
$& the entire expression matches the text, equivalent to
The text matched by the capture grouping of the $ $ corresponding number
${name} corresponds to named capture group matching text
$ ' text before matching text in the target string
$ ' text after matching text in the target string
$$ Single $ character ($ display as $$!)
$_ a copy of the regular original target string
$+. NET represents the last captured bracket matching text
Staticvoid Main (String[] args) {Regex REG1 =New Regex (@"\d+");String str = REG1. Replace ("123","Insert into table where id = $&"); Console.WriteLine (str);//Output insert into table where id = 123Regex REG2 =New Regex (@"1\+1= (\d)");String str2 = Reg2. Replace ("1+1=3","Not $"); Console.WriteLine (STR2);//Output is not 3Regex reg3 =New Regex (@"1\+1= (? <result>\d)");String str3 = Reg3. Replace ("1+1=3","Not ${result}"); Console.WriteLine (STR3);//Output is not 3Regex REG4 =New Regex (@"\d+");String STR4 = Reg4. Replace ("123ABC","The back is $ '");//Text after matching text Console.WriteLine (STR4);//The output is followed by abcabc why is the output followed by ABCABC? Because $ ' means ABC, then replace 123 in the original string. I don't know how many more times I can say this.Regex Reg5 =New Regex (@"\d+");String STR5 = Reg5. Replace ("ABC123",""); //ABC Front is the ABC symbol is 1 left that new Regex (@ " \d+); string STR6 = Reg6. Replace ( "abc123", Span style= "color: #800000;" > " right raw input string $_" ); Console.WriteLine (STR6); //
The regular assembly in. NET is used to build regular expression libraries, saved on the hard disk, other programs can also be called, improve the reuse rate. The main thing is to use the compiletoassembly method of the Regex class.
2013-4-26 micro-Rain, haze
Today, met a very interesting question, the company more than a customer, the product said add keywords too hard, let me help batch import a number of keywords. Brother these days just in the study of the regular expression, so apart, immediately should come down. A look, Excel, forget Npoi haven't learned yet. It is then copied into the txt text.
The format is as follows:
Zhongshan Road
Yue Ken Road
.....
Heaven help me also, difficult, and it seems that these days to learn something useful. So I immediately have the following code
Staticvoid Main (String[] args) {string str = file.readalltext (@ "D:\daoru.txt", Encoding.default); Regex reg = new Regex (@ ". +"); string str1 = Reg. Replace (str, "insert into Keyword values (196, ' admin1 ', ' admin1 ', ' $& ')"); File.writealltext (@ "D:\123.txt", str1); Console.readkey ();}
This is a method based on the keyword generation of SQL statements, import txt text from the D disk (in this place, encountered a problem, because the key word is Chinese, so intuitively feel it should be used Utf-8 code to read, but unexpectedly wrong. So the Internet check, incredibly with Encoding.default can solve this problem). The regular expression is then used to match the keyword. The default new Regex () dot number. It doesn't match the newline character, so it's perfect for a single line of keywords, such as when copying from Excel. Then replace the keyword with a SQL statement with the replace provided by the Regex class, and paste it directly onto the database for full selection and execution. Ok. Nearly 500 keywords were imported at one time.
2013-4-27 Sunny
I thought the regular expression was good, the result of yesterday's SQL statement replaced the problem, the data stored in the database for no reason more than a newline character. In fact, when executing SQL statements, SQL Server has been very conscientious to give hints, but too careless or happy too early to ignore directly. Look at the picture when the SQL statement was executed yesterday:
See the line, so that will be in the result of a more than a \ r, in the database table can not see, but in use, if only for display, but also no problem, but if used to match, it is tragic. So today the program is changed. To replace a newline character. The code changes to the following, where the red flag is changed:
Staticvoid Main (String[] args) {String str = File.readalltext (@"D:\daoru.txt", Encoding.default); Regex reg = new Regex (@ ". +"); string str1 = Reg. Replace (str, "insert into Jm_sinablog_keyword values", ' jmeii ', ' jmeii ', ' $& ')"). Replace (char), (char) 0); Here File.writealltext (@ "D:\123.txt", str1); Console.readkey (); }
In this way, you replace the line break. By copying the generated code into SQL Server, you can see that the SQL Server display has changed:
This is no problem, in the future when writing regular expressions to swap lines, the space is very sensitive to the line.
Regular Expressions in C #