In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what are the skills of using regular expressions". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the skills for using regular expressions?"
Regex novice on the road
In essence, regular expressions are sequences of characters that define a search pattern. Regular expressions are commonly used in tools such as grep to find patterns in longer text strings.
Consider the following cat.txt file:
Catcat2 dog
If we use the regular expression cat to search for matches, we will find the following matches:
Catcat2
Advanced users should note that there is a technical error in this article, that is, regular expressions are confused with tools that use regular expressions, such as grep.
Regular expressions apply to characters, not words
An important issue to be emphasized over and over again is that regular expressions apply to characters, not words. Implied concatenation.
If we use the regular expression search pattern cat, we don't look for the word "cat", but for the characters c, a, and t.
Dots and asterisks
The most basic characters are single characters, such as a, b, c, etc. Now let's introduce the following two special characters.
. (dot) characters can match * any single character *. For example, if we search for c.t, it will match anything from cat to c0t or cAt, and will match any single character c + any character + single character t.
The * (asterisk) character is a bit difficult. It modifies the character in front of it and then matches the * zero or more characters * of that character. That's true. For example, cat* can match cat, catt, cattttt, and ca.
Example Analysis: The cat ate my homework
Suppose we read a file line by line, and the first line looks like this:
The cat ate my homework.
Let's see how to match the pattern cat in this row.
We first match the first character of the pattern with the first character in the sentence.
If no match is found, jump to the next character in the line, and then start with the first character of the pattern.
If we find a match, we jump to the pattern and the next character in the line, and then repeat the process. When we find a match for the entire pattern, we return the row that found the match.
This is the most basic and common function of regular expressions, which is to find smaller search patterns in larger strings.
At this point, I think you have a rough idea of what a regular expression is and its two special characters:. (dot) and * (asterisk). Next, I will introduce you to more other content.
Regular expression Trident
Each part of a regular expression can consist of three different components:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Anchor point
Character set
Modifier
These three parts make up the Trident of regular expressions!
Let's start with the first part of Trident: anchor!
Anchor point
The anchor point specifies the mode location of each row. Here are the two most important anchor points:
^ (caret) pins the pattern to the beginning of the line. For example, the pattern ^ 1 matches any line that starts with 1.
$(dollar sign) fixes the pattern to the end of the sentence. For example, 9$ matches any line that ends in 9.
Note that in both cases, the anchor must be at the beginning and end of the pattern, respectively. ^ 1 matches the first 1 of the line, but 1 ^ matches the 1 of ^. Similarly, 1$ matches a line that ends with 1, but $1 matches a dollar sign followed by 1 anywhere on that line.
Character set
The second part of Trident: character set. The character set is the basis of regular expressions. A single character, such as a, is the most basic character set (a set of elements). But regular expressions such as [0-9] can match any number, or if you can recall the meaning of *, you can make a pattern [0-9] [0-9] (the content of this pattern match is left to the reader as an exercise).
Other important character sets:
[0-9] match 0... Any number in 9
[amurz] matches any lowercase letter
[Amurz] matches any uppercase letter
We can also combine multiple character sets:
[A-ZA-Z0-9] matches any uppercase and lowercase letters and a single number.
Modifier
This section does not expand in depth, taking a modifier * (asterisk) encountered earlier as an example. The modifier changes the meaning of the characters before it. There are many other modifiers, but taking * as an example is a good start.
As follows: let's quickly dump the text to a file.
$echo "The cat jumps long time\ nThen we also have the fact that these are words.\ n1234 this is a test post please ignore." > > grep.txt
This is what is now in the file.
$cat grep.txt The cat jumps long timeThen we also have the fact that these are words. 1234 this is a test post please ignore.
Looking for cat.
$grep "cat" grep.txt The cat jumps long tim
Look for any line that starts with the number ^ [0-9].
$grep "^ [0-9]" grep.txt 1234 this is a test post please ignore. At this point, I believe you have a deeper understanding of "what are the skills for using regular expressions?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.