(2) regular expressions to locate characters

Source: Internet
Author: User

"Positioning character" represents a virtual character, which represents a location, you can also intuitively think that "positioning character" represents the tiny gap between a character and character.
^ Indicates that the character after it must be at the beginning of the string
$ Indicates that the character before it must be at the end of the string
\ B matches the boundary of a word
\ B matches a non-word boundary
In addition, the character before \ A must be at the beginning of the character, and the character before \ Z must be at the end of the string, the character before \ Z must be at the end of the string or before the line break


The following provides some simple examples:

Code
String I = "Live for nothing, die for something ";
RegEx R1 = new RegEx ("^ live for nothing, die for something $ ");
// R1.ismatch (I) True
RegEx r2 = new RegEx ("^ live for nothing, die for some $ ");
// R2.ismatch (I) False
RegEx R3 = new RegEx ("^ live for nothing, die for some ");
// R3.ismatch (I) True

String I = @ "Live for nothing,
Die for something "; // multiple rows
RegEx R1 = new RegEx ("^ live for nothing, die for something $ ");
Console. writeline ("R1 match count:" + r1.matches (I). Count); // 0
RegEx r2 = new RegEx ("^ live for nothing, die for something $", regexoptions. multiline );
Console. writeline ("R2 match count:" + r2.matches (I). Count); // 0
RegEx R3 = new RegEx ("^ live for nothing, \ r \ ndie for something $ ");
Console. writeline ("R3 match count:" + r3.matches (I). Count); // 1
RegEx r4 = new RegEx ("^ live for nothing, $ ");
Console. writeline ("R4 match count:" + r4.matches (I). Count); // 0
RegEx R5 = new RegEx ("^ live for nothing, $", regexoptions. multiline );
Console. writeline ("R5 match count:" + r5.matches (I). Count); // 0
RegEx R6 = new RegEx ("^ live for nothing, \ r \ N $ ");
Console. writeline ("R6 match count:" + r6.matches (I). Count); // 0
RegEx R7 = new RegEx ("^ live for nothing, \ r \ N $", regexoptions. multiline );
Console. writeline ("R7 match count:" + r7.matches (I). Count); // 0
RegEx R8 = new RegEx ("^ live for nothing, \ r$ ");
Console. writeline ("R8 match count:" + r8.matches (I). Count); // 0
RegEx R9 = new RegEx ("^ live for nothing, \ r$", regexoptions. multiline );
Console. writeline ("R9 match count:" + r9.matches (I). Count); // 1
RegEx R10 = new RegEx ("^ die for something $ ");
Console. writeline ("R10 match count:" + r10.matches (I). Count); // 0
RegEx R11 = new RegEx ("^ die for something $", regexoptions. multiline );
Console. writeline ("R11 match count:" + r11.matches (I). Count); // 1
RegEx R12 = new RegEx ("^ ");
Console. writeline ("R12 match count:" + r12.matches (I). Count); // 1
RegEx R13 = new RegEx ("$ ");
Console. writeline ("R13 match count:" + r13.matches (I). Count); // 1
RegEx R14 = new RegEx ("^", regexoptions. multiline );
Console. writeline ("R14 match count:" + r14.matches (I). Count); // 2
RegEx R15 = new RegEx ("$", regexoptions. multiline );
Console. writeline ("R15 match count:" + r15.matches (I). Count); // 2
RegEx R16 = new RegEx ("^ live for nothing, \ r$ \ n ^ die for something $", regexoptions. multiline );
Console. writeline ("R16 match count:" + r16.matches (I). Count); // 1
// For a multi-line string, after the multiline option is set, ^ and $ match multiple times.

String I = "Live for nothing, die for something ";
String M = "Live for nothing, die for some thing ";
RegEx R1 = new RegEx (@ "\ bthing \ B ");
Console. writeline ("R1 match count:" + r1.matches (I). Count); // 0
RegEx r2 = new RegEx (@ "thing \ B ");
Console. writeline ("R2 match count:" + r2.matches (I). Count); // 2
RegEx R3 = new RegEx (@ "\ bthing \ B ");
Console. writeline ("R3 match count:" + r3.matches (M). Count); // 1
RegEx r4 = new RegEx (@ "\ BFOR something \ B ");
Console. writeline ("R4 match count:" + r4.matches (I). Count); // 1
// \ B is usually used to constrain a complete word

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.