Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use special characters in Perl pattern matching

Shulou Source: shulou.com Published: 2022-06-02 07:56:56 09月12日 Update

This article mainly shows you "how to use special characters in Perl pattern matching". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn how to use special characters in Perl pattern matching.

Special characters in Perl pattern matching

1.1 "+"

+ is used to match the preceding character with the following character at least once, or any time. Similar to windows * for example, dog,dogg,dooooog/ab+/ that / d+g/ can match will match abb, not ab, in the string abbc.

1.2 [] and [^]

[] means to match one of a set of characters

^ represents all characters except them

For example, / w [abcd] s / was,wbs,wcs,wds that can match

/ w [^ ab] s / mismatched was,wbs

It can also be written in the following form

/ w [a-z0-9A-Z] s /

1.3 characters * and?

They are similar to + except that * matches 0, 1 or more of the same characters,? Matches 0 or 1 of the character.

For example, / de*f/ matches df, def, deeeef, and so on; / de?f/ matches df or def.

1.4 position wildcard (anchor mode)

^ or\ An only matches the string head.

$or\ Z only matches the tail

\ b match word boundaries

\ B word internal matching

For example: / ^ def/ only matches strings that start with def, / $def/ only matches strings that end in def, and the combined / ^ def$/ matches only the string def (?). \ An and\ Z are different from ^ and $when multiple lines match.

\ b match at word boundaries: /\ bdef/ matches words such as def and defghi that start with def, but does not match abcdef. / def\ b / matches words that end in def, such as def and abcdef, but does not match defghi,/\ bdef\ b / matches only the string def. Note: /\ bdef/ matches $defghi because $is not considered part of the word.

\ B match inside the words: /\ Bdef/ matches abcdef, etc., but does not match def;/def\ B / match defghi, etc.; /\ Bdef\ B / matches cdefg, abcdefghi, etc., but does not match def,defghi,abcdef.

Character escape class in 1.5Perl pattern matching

\ d any number [0-9]

\ d any character except a number [^ 0-9]

\ w any word character [_ 0-9a-zA-Z]

\ W any non-word character [^ _ 0-9a-zA-Z]

\ s blank [\ r\ t\ n\ f]

\ s is not blank [^\ r\ t\ n\ f]

1.6 "."

Character "." Matches all characters except line breaks, usually in conjunction with *.

Matches a specified number of characters in 1.7Perl pattern matching

Character pair {} specifies the number of occurrences of matched characters.

For example: / de {1pr 3} f / match def,deef and deeef

/ de {3} f / match deeef

/ de {3,} f / match no less than 3 e between d and f

/ de {0Pert 3} f / matches no more than 3 e between d and f.

1.8 specify options

The character "|" specifies two or more selections to match the pattern. For example, / def | ghi/ matches def or ghi.

These escape characters have a certain order of operation:

() Mode memory

+ *? {} occurrence times

^ $\ b\ B anchor

| | options |

Pattern order variables in 1.9Perl pattern matching

The result of calling the reuse part after Perl pattern matching is available with the variable $n, and all results with the variable $&.

$string= "Thisstringcontainsthenumber25.11."

$string=~/-? (\ d +)\.? (\ d +) /; # matching result is 25.11

$integerpart=$1;#now$integerpart=25

$decimalpart=$2;#now$decimalpart=11

$totalpart=$&;#nowtotalpart=25.11

The above is all the content of the article "how to use Special characters in Perl pattern 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!

Tags: Characters patterns words strings special content variables articles results between multiple numbers times whitespace escape boundaries parts learning help different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi Microsoft Shulou Tech Info Shulou Information