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 to realize Perl regular expression matching

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to achieve Perl regular expression matching", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to achieve Perl regular expression matching" this article.

Detailed explanation of Perl regular expression

There are three forms of Perl regular expressions: match, replace, and transform. Then give a detailed explanation of each expression.

Perl regular expression matching: the form m _ stroke / indicates that the regular expression inside / / will be used to match the scalar to the left of = ~ or! ~. For grammatical simplification, omit m.

Perl regular expression replacement: this form indicates that the Perl regular expression will be replaced by text, omitting s with / / for syntax simplification.

Perl regular expression conversion: the tr/// form contains a series of characters-/-and replaces them with.

Note that the transformation is not really an Perl regular expression, but it is often used to manipulate data that is difficult to handle with Perl regular expressions. Therefore, tr/ [0-9] / 9876543210. Make up 1223456789J.987654321 and other strings.

◆ binds these expressions to scalars by using = ~ (in English: does, the same as "match") and! ~ (English: doesn't, the same as "mismatch"). As an example of this type, here are six examples of Perl regular expressions and their corresponding definitions:

$scalarName=~s/a/b;#substitutethecharacteraforb,andreturntrueifthiscanhappern $scalarName=~m/a;#doesthescalar$scalarNamehaveanainit? $scalarName=~tr/A-Z/a-z/;#translateallcapitalletterwithlowercaseones,andreturntureifthishappens $scalar names indexed. $scalarNamethorax is responsible for the return of turnfalseifits.Scalar Nameplate accounts for a return falseifit.Scalar Namematchthecharactera exists. $scalarNameplate tracing trash 0-9 Accord a MuhrjAccord translatethedigitsforthelettersathruj andfalseifthishappens

. If we enter code like hornedtoad=~m/toad/, the situation shown in figure 9-1 appears:

In addition, if the reader is matching a specific variable $_ (the reader may use it in the while loop, map, or grep), you don't have to! ~ and = ~. Therefore, all of the following code will be legal:

My@elemente= ('al','a2','a3','a4','a5'); foreach (@ elements) {s _ Unix _ r _ b;}

The program makes @ elements equal to b1 elements, b2. B3, 4, and 5. In addition:

While () {printif (m/ERBOR/);}

Print all lines that contain error strings:

If (grep (/ pattern/,@lines)) {print "thevariable\ @ lineshaspatterninit!\ n";}

Print all lines that contain the content of the schema pattern, which directly introduces the next principle.

◆ Perl regular expressions match.

Perl regular expressions always match on scalars. Note the importance of scalars here. If the reader tries the following code:

@ arrayName= ('variablel','variable2'); @ arrayNameworthy variables in the arrayNameplate trees

So @ arrayName match failed! @ arrayName is interpreted by Perl as 2, so this means that the reader is typing:

'2'=~m/variable/

At the very least, this does not give the expected results. If the reader wants to do this, the loser is:

Grep (maxim variablelemarrayName)

This function loops through each element in @ arrayName, returns the number of matches (in a scalar environment), and returns an actual list of matching elements in an array environment.

◆ for a given pattern string, the Perl regular expression matches only the earliest matches. The default for matching is to match or replace once at a time.

This principle uses a process called "backtracking" to indicate how to match a given string; if a local match is found to invalidate that match, the Perl regular expression "backtracks" the smallest possible number of characters in the string to ensure that no matches are lost.

This principle is the most helpful for understanding what Perl regular expressions are doing, and you don't need to understand what it is doing in the same form as Perl. Suppose you have the following pattern: 'Sillypeopledosillythingsifinsillymoods'

Also want to match the following pattern: 'sillymoods'

So the Perl regular expression engine matches silly and then encounters the P of people. At this point, the Perl regular expression engine knows that * silly mismatches, so the Perl regular expression engine moves to P and continues to look for matches. It then encounters a second silly and matches the moods. However, you get the letter t (in thing), so move to t in things to continue Perl regular expression matching. When the engine encounters the third silly and tries to match the moods, the match is successful and the match is completed. Backtracking becomes more important when we encounter wildcards. If there are several wildcards in the same Perl regular expression and all the wildcards are intertwined, then there is a morbid situation, in which case backtracking becomes very expensive. Look at the following expression: $line=m/expression.*matching.*could.*be.*very.*expensive.*/

◆ * stands for a wildcard, which means "match any character (except newline characters) zero or more times." This process can take a long time; if it is possible to match at the end of an unmatched string, the engine will backtrack frantically. For more information on this, please pay attention to the principles on wildcards.

If the reader finds a situation similar to the above, then the wildcard needs to decompose the Perl regular expression into small work parts. In other words, simplify your Perl regular expressions.

The above is all the content of the article "how to achieve Perl regular expression matching". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report