In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Regular expression, also known as regular expression, regular expression (English: Regular Expression, often abbreviated as regex, regexp or RE in code), is a concept of computer science. Regular expressions use a single string to describe and match a series of strings that conform to a syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that matches a certain pattern. Wikipedia
Regular expression is an embodiment of computer intelligence, it can let us find what we want most in the complex computer text, under her regular and hazy veil is an elegant and mischievous face, for men who just enter the Linux door, all bow under her pomegranate skirt, and all this article is to do is how to capture her heart.
First, know her life background.
In 1956, an American mathematical scientist named Stephen Kleene, based on the early work of Warren McCulloch and Walter Pitts, published a paper entitled "representation of Neural Network events". He used mathematical symbols called regular sets to describe the model and introduced the concept of regular expressions. Regular expressions are used as an expression to describe what it calls "algebra of regular sets", so the term "regular expressions" is adopted. After a period of time, people found that the results of this work could be applied to other aspects. Ken Thompson applied this result to some early research on computational search algorithms. Ken Thompson was the main inventor of Unix and the father of the famous Unix. The father of Unix introduced this symbolic system into the editor QED, then the editor ed on Unix, and finally grep. Over the past two decades, in the WINDOW camp, the idea and application of regular expressions have been supported and embedded in most Windows developer kits! At present, the graceful dance of regular expressions can be seen in the mainstream development languages (PHP, C#, Java, C++, VB, Javascript, Ruby and python, etc.) and in trillions of applications.
2. Unveil her
Well, after the last inode, we came across another thing that people don't know how to start with, but we still have to learn from her anyway, so let's take a look at what regular expressions have:
Basic regular expression metacharacters:
Character matching:
. Match any single character
[]: matches any single character in the specified range
[^]: matches any single character outside the specified range
[0-9], [: digit:], [^ 0-9], [^ [: digit:]]
[a murz], [[: lower:]]
[Amurz], [[: upper:]]
[[: space:]]
[[: punct:]]
[0-9a-zA-Z], [[: alnum:]]
[a-zA-Z], [[: alpha:]]
Degree matching: a control character is provided after the expected matching character to express the number of times it matches the specified number of characters preceding it
*: any length, indicating 0, 1, or more times
. *: any character of any length
Working in greedy mode
\?: 0 or 1 time; indicates that the character on the left side is optional
\ +: one or more times; indicates that the character on the left side appears at least once
\ {m\}: M times; indicates that the character on the left side appears exactly m times
At least m times and no more than n times
\ {0magnetic n\}: n times at most
\ {m,\}: at least m times
Position Anchor:
^: anchor the beginning of the line
$: anchor the end of the line
^ $: match blank lines
Word anchoring: a continuous string of non-special characters
\
< :锚定词首,也可用\b \>Anchor the suffix, or you can use\ b
\: match the whole word that PATTERN can match
Grouping:\ (\)
\ 1,\ 2.\ n: used to refer to the contents of the previous grouping
Extended regular expression:
Character matching:
.
[]
[^]
Number of times match:
*: any time
0 or 1 time
+: at least once
{m}: exact match m times
{mdirection n}: at least m times, up to many times
{m,}: at least m times
{0magnetic n}: up to multiple times
Position Anchor:
^
$
\,\ b
Grouping:
()
Reference:\ 1,\ 2,.
Or:
A | bazoo an or b
Or everything on both sides.
Third, force a kiss on her
Well, I know it may come as a surprise, but forcibly kissing a girl is the quickest way to get an answer, isn't it? OK, if you want to kiss us, you need to use both hands and mouths. All right, let's see what kind of hands and mouths we can use. When we learn about the life history of the goddess, we know that our father of Unix introduced grep, which includes grep, egrep and fgrep in the grep family of Unix. The commands of egrep and fgrep are only slightly different from grep. Egrep is an extension of grep that supports more metacharacters. Fgrep treats all letters as words, that is, metacharacters in regular expressions express their own literal meaning and are no longer special.
All right, let's get our hands and mouths ready for action:
Let's start with a simple action step by step-- > find the lines in / proc/meminfo that begin with s | S.
Our use of one hand is grep [OPTIONS] PATTERN [FILE...], where ^ is the beginning of the anchor line, and [sS] means that only the letters s and S can be selected in this collection.
Let's have another one-- > find out one or two digits in the / etc/passwd file. In order to make our actions less pale, let's give her some color to see.
-- the color option indicates that the searched content is displayed in color,\ these two can be combined to determine a word, [0-9] indicates that a number range is selected between 0-9, and\? Indicates that the character before it can appear once or not.
Our movements are becoming more and more proficient-- > Let's give a few sentences to see how to use grouping.
We output four sentences, two of which are similar before and after, and we find them by grouping and quoting:\ (l. E\). *\ 1 represents the middle root of two arbitrary letters beginning with l, and then regards this as a whole,. * represents a character of any length, and\ 1 refers to the previous reference to the content enclosed in\ (\).
If we want to use colored output all the time, we can make it less troublesome in the future by defining aliases:
[root@bogon] # alias grep='grep-- color'
Next, let's do something difficult-- > take out the path name of a path.
Grep-o indicates that only the selected content is displayed. Here we use grep twice, the first time [^ /] represents the character that finally matches the character that does not end with /, and / .* / represents the content between the two separators. If we want to get a format similar to the dirname command, we can use it.
[root@bogon ~] # echo'/ etc/rc.d/init.d/' | grep-o'/. * [^ /]'| grep-o'/. * /'| grep-o'/. * [^ /] 'you can try it.
We can't use one hand all the time, it's time to know the other-- > take the base name of a path.
Egrep equals grep-E, both represent extended regular expressions, and + indicates that the [^ /] in front of it occurs one or more times,? As we explained earlier, $means anchor the end of the line, with the path name of / at the end, and we can just cut it with cut.
Let's consolidate the results again:
The first is to find the content with the same name but inconsistent suffix in the string; the second is to find the content in the string that conforms to the mailbox format. Let's experience it, and I won't explain it any more. In order to avoid becoming a "three hands", I will not introduce fgrep, which is actually very simple, which is a match of characters as they are. After these three strong "pick-up" steps, I think you should be able to capture her heart, but if you want to marry this cunning goddess home, you need to make more efforts to know yourself and the enemy.
Well, I hope this article can help you. Please look at the inadequacies of this article. Thank you.
Use the man command to take a look at how to use PS:grep,egrep,fgrep. I won't repeat it in this article.
[root@bogon ~] # echo-e 'adsfqw@163.com\ nqwqe@qq;com\ nFreedom@gmail.com.com\ nadfa.msn.com' | egrep-- color'[[: alnum:]] + @ [[: alnum:]] +\. [[: alpha:]] + $'--> modify the BUG of the extracted mailbox
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.