Global match Mode

Source: Internet
Author: User

-- Start

Case-insensitive matching mode

InCase InsensitiveThis section describes how to ignore case-insensitive matching in emeditor. The following describes how to perform case-insensitive matching in Perl and Java. Perl uses the/I modifier and Java uses pattern. case_insensitive.

#!/usr/bin/perlmy $testText = "I love regular expressions.";if($testText =~ m/REGULAR/i) {print "finds the word.";} else {print "cannot find the word.";}

public static void main(String[] args) {String testText = "I love regular expressions.";Pattern p = Pattern.compile("REGULAR", Pattern.CASE_INSENSITIVE);Matcher m = p.matcher(testText);if (m.find()) {System.out.println("finds the word.");} else {System.out.println("cannot find the word.");}}

Comment Mode

Some regular expressions are very complex. If there is no comment, we can hardly understand it. To add a comment to a regular expression, we must use the comment mode. Perl uses the/x modifier and Java uses pattern. Comments.

#! /Usr/bin/perlmy $ testtext = "I love regular expressions."; if ($ testtext = ~ M/# matching r. Here, comment R # matching E. Here, comment e # matching G. Here, comment g # matching U. Here, comment U # matching L, note L # match a. Note A # match R. Note R/x) {print "finds the word. ";} else {print" cannot find the word. ";}
Public static void main (string [] ARGs) {string testtext = "I love regular expressions. "; string Regexp = "# matching r \ n" + "R" + "# matching e \ n" + "E" + "# matching g \ n" + "G" +" # matching U \ n "+" U "+" # matching L \ n "+" L "+" # matching a \ n "+" A "+" # matching r \ N "+" R "; pattern P = pattern. compile (Regexp, pattern. comments); matcher M = P. matcher (testtext); If (M. find () {system. out. println ("finds the word. ");} else {system. out. println ("cannot find the word. ");}}

Single-line mode, also known as Dot-match-all match Mode)

InMatch any character: dot (.)This section describes that the DoT number can match any character. In fact, this sentence is not accurate. Generally, the dot cannot match the line break. To make the dot match the line break, we must use the single line mode. Perl uses the/s modifier and Java uses pattern. dotall.

#!/usr/bin/perlmy $testText = "I love reg\nular expressions.";if($testText =~ m/reg.ular/s) {print "finds the word.";} else {print "cannot find the word.";}

public static void main(String[] args) {String testText = "I love reg\nular expressions.";String regExp = "reg.ular";Pattern p = Pattern.compile(regExp, Pattern.DOTALL);Matcher m = p.matcher(testText);if (m.find()) {System.out.println("finds the word.");} else {System.out.println("cannot find the word.");}}

Multiline mode, also known as enhanced line-anchor match Mode)

In the program, ^ and $ are used to match the start and end positions of the string. However, in general, they do not recognize linefeeds inside strings. That is to say, the following regular expression cannot match the following text.

Expression: ^ regular text: I love \ nregular expressions

To enable ^ and $ to recognize line breaks, we can use multiline mode. Perl uses the/M modifier and Java uses pattern. multiline.

#!/usr/bin/perlmy $testText = "I love \nregular expressions.";if($testText =~ m/^regular/m) {print "finds the word.";} else {print "cannot find the word.";}
public static void main(String[] args) {String testText = "I love \nregular expressions.";String regExp = "^regular";Pattern p = Pattern.compile(regExp, Pattern.MULTILINE);Matcher m = p.matcher(testText);if (m.find()) {System.out.println("finds the word.");} else {System.out.println("cannot find the word.");}}

What if I want to match the start and end positions of a string in multiline mode? For this reason, regular expressions also provide \ A and \ Z, which are used in the same way as normal ^ and $, but in multi-row mode, their meaning will not change, that is to say, \ A and \ Z will never recognize line breaks. In fact, whatever the mode, there is also a metacharacter to match the end position of the string, that is, \ Z. In most tools that support regular expressions, it is no different from \ Z.

To sum up, the single-row mode and multi-row mode do not have any relationship, but from the perspective of name, we always think they have a relationship. You think too much.

Literal text)

In this mode, any character represents itself. In fact, it is equivalent to not using a regular expression. To use this mode, add \ q and \ e before and after the expression in Perl, and use pattern. Literal in Java.

#!/usr/bin/perlmy $testText = "I love regular expressions.";if($testText =~ m/\Qreg.lar\E/) {print "finds the word.";} else {print "cannot find the word.";}

public static void main(String[] args) {String testText = "I love regular expressions.";String regExp = "reg.lar";Pattern p = Pattern.compile(regExp, Pattern.LITERAL);Matcher m = p.matcher(testText);if (m.find()) {System.out.println("finds the word.");} else {System.out.println("cannot find the word.");}}

Mixed use of multiple modes

In fact, we can use multiple modes in a mix. Let's look at the example below.

#!/usr/bin/perlmy $testText = "I love \nregular expressions.";if($testText =~ m/^REGULAR/ism) {print "finds the word.";} else {print "cannot find the word.";}
public static void main(String[] args) {String testText = "I love \nregular expressions.";String regExp = "^REGULAR";Pattern p = Pattern.compile(regExp, Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE);Matcher m = p.matcher(testText);if (m.find()) {System.out.println("finds the word.");} else {System.out.println("cannot find the word.");}}

--For more information, see:Regular Expressions
--Shengming: reprinted, please indicate the source
-- Last updated on 2012-05-12
-- Written by shangbo on 2012-05-02
-- End

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.