Detailed explanation of matching a single character in the regular expression tutorial, detailed explanation of the Regular Expression

Source: Internet
Author: User

Detailed explanation of matching a single character in the regular expression tutorial, detailed explanation of the Regular Expression

This document describes how to match a single character in the regular expression tutorial. We will share this with you for your reference. The details are as follows:

Note: In all examples, regular expression matching results are included between [and] in the source text, and some examples are implemented using Java. If the regular expression is used in java, it will be described in the corresponding area. All java examples have passed the test under JDK1.6.0 _ 13.

Java test code:

/*** Based on the regular expression and the source text to be matched, output matching result * @ param regex Regular Expression * @ param sourceText Source Text to be matched */public static void matchAndPrint (String regex, String sourceText) {Pattern pattern = Pattern. compile (regex); Matcher matcher = pattern. matcher (sourceText); while (matcher. find () {System. out. println (matcher. group ());}}

1. Match plain text

1. Only one matching result exists.

First, let's look at a simple regular expression, today. Although it is plain text, it is a regular expression. Let's look at an example:

Source Text: Yesterday is history, tomorrow is a mystery, but today is a gift.

Regular Expression: today

Result: Yesterday is history, tomorrow is a mystery, but [today] is a gift.

Analysis: the regular expression used here is plain text, which matches the today in the source text.

Call the matchAndPrint method. The output result is:

Today

2. Multiple matching results

Source Text: Yesterday is history, tomorrow is a mystery, but today is a gift.

Regular Expression: is

Result: Yesterday is history, tomorrow is a mystery, but [today] is a gift.

Analysis: In the source text, there are three is, but four are output, because the is in history will also be matched.

Call the matchAndPrint method. The output result is:

Is

Is

Is

Is

3. Case sensitivity

Regular Expressions are case-sensitive, but many regular expressions also support case-insensitive matching. In JavaScript, the I flag is used to perform a case-insensitive match. In java, if it is case insensitive, you can specify:

Patternpattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

2. match any character

The regular expressions mentioned above are static plain text, and they do not reflect the power of regular expressions. Next, let's take a look at how to use regular expressions to match unpredictable characters.

In regular expressions, special characters (or character set combinations) are used to give the information to be searched .. Character (English state period) can match any single character. Which is equivalent? And _ (underline) characters in SQL. For example, the regular expression c. t matches cat, cut, cot, and so on. The following is an example.

Text:

Orders1.txt

Orders2.txt

Sales1.txt

SalesA.txt

Orders3.txt

Sales2.txt

Sales.txt

Regular Expression: sales.

Result:

Orders1.txt

Orders2.txt

【Sales1).txt

【Salesaapps.txt

Orders3.txt

【Sales2).txt

[Sales.] txt

Analysis: the regular expression sales will find the file name consisting of the string sales and another note. From the result, we can see that it matches letters, numbers, and itself. Four of the seven files match this pattern.

If you call the matchAndPrint method, the output result is:

Sales1

SalesA

Sales2

Sales.

3. Match special characters

. Characters have special meanings in regular expressions. If you need a. Character in the regular expression, you must find a way to tell the regular expression that you need a. character rather than its special meaning in the regular expression. To this end, you must add the \ character before. to escape it. \ Is also a metacharacter, indicating that this character has a special meaning, rather than the meaning itself ). Let's look at the example below.

Find the file starting with na or sa, No matter what number it follows.

Text:

Sales.txt

Na1.txt

Na2.txt

Sa1.txt

Sanatxt.txt

Regular Expression: .a..txt

Result:

【Sal】es.txt

【Na1g0.txt

【Na22.16.txt

【Sa1】.txt

【Sanatxt0000.txt

Analysis: This document finds out na1.txtw.na2.txt=sa1.txt, but also finds two unexpected results. The. Character in the regular expression ".a..txt" will match any character. To match the. character itself, use \ escape. Modifying the regular expression to. a. \. txt can meet our needs.

NOTE: If java is used, the regular expression. a. \. txt should be written as. a. \. txt, because \ is also an escape character in java.

Iv. Summary

Regular Expressions are generally referred to as pattern. They are actually character strings. These characters can be common characters (plain text) or metacharacters (special characters with special meanings ). This section describes how to use common characters and metacharacters to match unit characters .. It can match any character. \ Is used to escape characters. In regular expressions, character sequences with special meanings always start with \ characters. In the following article, we will introduce how to match a group of characters.

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

I hope this article will help you learn regular expressions.

Related Article

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.