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 use Perl regular expressions

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use Perl regular expressions, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Perl regular expression

Metacharacters of Perl regular expressions:

. Match any character except\ n

\ escape character

Quantifier for Perl regular expression:

{3} matches the three *? {0,} of the previous unit? Match zero or more of the previous unit

+ {1,}? Match one or more of the previous unit

?? {0,1}? Match the zero or one of the previous unit

* {0,} matches multiple or zero greed of the previous unit

+ {1,} matches multiple or one greed of the previous unit

? {0graine 1} matches one or zero greed of the previous unit.

Grouping of Perl regular expressions:

() multiple options for marshalling and triggering memory (?:) marshalling but not triggering memory Perl regular expressions:

Grammar and operator precedence of Perl regular expressions:

'()','(?)'>

'*','*?','+','+?','?,','{}','{}?'>

'' >

'|'

->'|'|

-> |

-> ('*'|'+'|'?'|'{'}') |

->'('')'| (?:'')'|

Character class for Perl regular expressions:

[abcwxyz] = = [a-cw-z]

\ dbath = [0-9]

\ During = [^ 0-9]

\ wicked = [a-zA-Z0-9 _]

\ wow = [^ a-zA-Z0-9 _]

\ skeeper = [\ f\ t\ n\ r]

\ Shop = [^\ f\ t\ n\ r]

Option modifier for Perl regular expression:

/ I make the match case insensitive

/ s make. Matches any character, including'\ ncharacters, which is equivalent to [\ d\ D]

/ x allows Perl regular expressions to use whitespace and comments to enhance readability. Space text, tab text, and so on will be ignored and can only be represented by\ s,\ t, etc. When / m matches a multiline target string, the meaning of ^ and $is changed to the beginning and end of the line anchor.

/ g No overlap global replacement modifier

Anchor for Perl regular expressions:

^ string head anchor

$tail anchor

\ b word boundary anchor reference\ w

\ B non-word boundary anchor

Binding operator for Perl regular expression:

= ~ non-assignment operator, also bind operator, It's says ing: don't deal with $_, deal with the guy on my left, Perl regular expression interpolation:

Matching variables of Perl regular expressions with double-reference interpolation rules

() used not only for marshalling Perl regular expressions, but also for triggering the memory of the matching engine, which stores the part of the entire matching segment that corresponds to the Perl regular expression in parentheses into the memory. From 1 number, quote to $1, 1, 5, 2, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4

Remember to end the entire matching segment of $& $MATCH when the next match is successful.

$`$substring before matching segment of PREMATCH

The substring after the $'$POSTMATCH matching segment

Examples of Perl regular expressions:

/ ^ fred | barney$/ either starts with fred or ends with barney string / ^ (fred | barney) $/

/ (wilma | pebbles?) /

/ ^ fred | barney$/m

Text processing replacement for Perl regular expressions:

Delimiter rule: s # ^ https://#http://#;

S {fred} {barney}

S [fred] (barney)

S#barney#

Binding operator: = ~

Case switch:\ U from here to\ E all uppercase

All lowercase from here to\ E

\ u the next letter is capitalized

\ l the next letter is lowercase

For example: s / (bush | mandola) /\ u\ L$1/ig

Split operator of Perl regular expression

Syntax: split/PATTERN/,EXPR,LIMIT

Split EXPR with / PATTERN/, and divide it into LIMIT at most. If LIMIT is omitted and the tail is empty string, discard it. If LIMIT is negative, it is regarded as infinite EXPR. Default is $_.

/ PATTERN/ defaults to /\ skeeper /

M _ hash in the context of a list of Perl regular expressions: returns variables in engine memory.

My$data= "BarneyRubbleFredFlintstoneWilmaFlintstone"

My%last_name= ($data=~/ (\ w +)\ s + (\ w +) / g)

My@arr= ($data=~/ (\ w +)\ s + (\ w +) / g)

Match multiple lines of text for Perl regular expressions:

^ and $are string head anchor and string tail anchor in normal mode, and line head anchor and line tail anchor in / m (multiline mode)

\ A match string head,\ Z match string tail

/ ^ nat/m===/ ^ Nat |\ nnat/

OpenFILE,$filename

Ordie "canopy topenails filenametically filled with flowers!"

My$lines=join''

$lines=~s/ ^ / $filename:/gm

Update multiple files for Perl regular expressions:

The magic of the diamond symbol: it will automatically open and close a series of files specified on the command line for you, and if the command line does not specify any file name, he will open standard input. The behavior of $^ I will change. If $^ I is not undef, the file will be renamed to File name.

"$^ I", then create a file named File name, then open the renamed file to read, and open the newly created file to write.

The above is all the content of the article "how to use Perl regular expressions". 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