Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How regular expressions match a single character

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how regular expressions match individual characters. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Java test code:

/ * * according to the regular expression and the source text to match, output the matching result * @ param regex regular expression * @ param sourceText source text to match * / 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. There is only one matching result

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 expressions: today

Results: 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, and the output is as follows:

Today

2. There are multiple matching results

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

Regular expressions: is

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

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

Call the matchAndPrint method, and the output is as follows:

Is

Is

Is

Is

3. The case of letters

Regular expressions are case-sensitive, but many regular expression implementations also support case-insensitive matching operations. In JavaScript, use the I flag to perform a case-insensitive match. In java, if you want to be case-insensitive, you can specify when compiling regular expressions:

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

Second, match any character

The regular expressions seen earlier are static plain text, and they don't show the power of regular expressions at all. Next, let's look at how to use regular expressions to match unpredictable characters.

In regular expressions, special characters (or sets of characters) are used to give something to search for. . Characters (English status period) can match any single character. Equivalent to the one in DOS? Characters and _ (underscore) characters in SQL. For example, the regular expression c.t will match cat, cut, cot, and so on. Let's look at an example.

Text:

Orders1.txt

Orders2.txt

Sales1.txt

SalesA.txt

Orders3.txt

Sales2.txt

Sales.txt

Regular expression: sales.

Results:

Orders1.txt

Orders2.txt

[sales1]. Txt

[salesA]. Txt

Orders3.txt

[sales2]. Txt

[sales.] txt

Analysis: regular expression sales. The file name consisting of the string sales and another note will be found. As you can see from the result,. Can match letters, numbers, and itself. Four of the seven files match this pattern.

If you call the matchAndPrint method, the output is:

Sales1

SalesA

Sales2

Sales.

Third, match special characters

. Characters have a special meaning in regular expressions. If you need a.. in the pattern, find a way to tell the regular expression what you need. The character itself rather than its special meaning in regular expressions. To this end, it must be in. It is escaped by the\ character before it. \ is also a metacharacter (metacharacter, indicating that the character has a special meaning, not the meaning of the character itself). Take a look at the following example.

Find the file that starts with na or sa, no matter what number it is followed by.

Text:

Sales.txt

Na1.txt

Na2.txt

Sa1.txt

Sanatxt.txt

Regular expression: .a..txt

Results:

[sal] es.txt

[na1]. Txt

[na2]. Txt

[sa1]. Txt

[sanatxt]. Txt

Analysis: this rule finds na1.txt, na2.txt, and sa1.txt, but also finds two unexpected results. Because. A. Txt is in the rule. The character will match any character. If you want to match. Character itself, then you need to use\ escape. Changing the regularity to .a.\ .txt can meet our needs.

Note: if you use java, the regular expression .a.\ .txt should be written as .a.\ .txt, because\ is also an escape character in the Java language.

Thank you for reading! This is the end of the article on "how regular expressions match a single character". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report