Perl string processing

Source: Internet
Author: User
Tags print format unpack

As we all know, Perl is very powerful in processing strings, and Perl (Practical Extraction and Reporting Language) is also very powerful in processing formats. Here we start to learn some Perl formats and string processing.

Familiar with the three most powerful functions: substr, pack, and unpack.

1. The case processing function LC (to lowercase) UC (to uppercase ).
$ Text = "zhengwen Feng ";
$ Text2 = Lc $ text;
$ Text3 = UC $ text;
Print "$ text2 \ n ";
Print "$ text3 \ n ";

2. Change the first letter to lower case (lcfirst) and the first letter to upper case (ucfirst ).
$ String = "zheng ";
$ String2 = lcfirst $ string;
$ String3 = ucfirst $ string;
Print "$ string2 \ n ";
Print "$ string3 \ n ";

3. Search for the string position (INDEX) in sequence and the string position (rindex) in reverse order ). Index finds the starting position of a substring in the string.
$ String = "Zheng Wen Feng ";
$ Position1 = index ($ string, "Wen ");
$ Position2 = index ($ string, "Wen", 10 );
$ Position3 = rindex ($ string, "Zheng ");
$ Position4 = rindex ($ string, "Zheng", 10 );
Print "$ position1, $ position2, $ position3, $ position4 \ n ";

#! /Usr/bin/perl
$ STR = "abcdefg1234567 ";
$ A = "12 ";
$ Pos = index ($ STR, $ );
Print $ POs, "\ n ";

[HTO @ localhost] $./tipind. pl
7

4. The substr function is very powerful. It can be used to directly replace a substring with a string.
$ Text = "Zheng wenfeng love kittykitty ";
$ Replace_to = "";
$ Replace_with = "like ";
Substr ($ text, index ($ text, $ replace_to), length ($ replace_to), $ replace_with );
Print "$ text \ n ";

5. String Length (number of characters)-length
#! /Usr/bin/perl
$ STR = "abcd99e ";
$ Strlen = length ($ Str );
Print $ strlen, "\ n ";
[Htog @ localhost] $./tiplen. pl
7

8. The pack and unpack functions are used to pack and unpack strings. They are very powerful and support string processing in multiple packaging formats.
$ Decimal = 17;
$ Newdecimal = unpack ("B32", pack ("N", $ decimal ));
Print "$ newdecimal \ n ";
$ String = "";
@ Array1 = unpack ("C *", $ string );
@ Array2 = unpack ("C *", $ string );
Print (join (",", @ array1), "\ n ");
Print (join (",", @ array2), "\ n ");
$ String1 = pack ("C *", @ array1 );
$ String2 = pack ("C *", @ array2 );
Print "$ string1 \ n ";
Print "$ string2 \ n ";

9. print format-sprintf
$ Value = 1234.56789;
Print sprintf "%. 4f \ n", $ value;

10. String comparison functions: eq, NE, CMP, LT, GT, le, and GE. Use CMP. You must never use '= = 'or EQ. The correct method is to use EQ regardless of the Perl string.
$ String1 = "Dahua ";
$ String2 = "Dahua ";
If ($ string1 EQ $ string2 ){
Print "$ sting1 = $ string2 \ n ";
}
If ($ string1 ne $ string2 ){
Print "$ string1! = $ String2 \ n ";
}

If ($ string1 CMP $ string2) = 0 ){
Print "$ string1 = $ string2 \ n ";
}

$ String1 = "zheng ";
$ String2 = "kitty ";
If ($ string1 lt $ string2 ){
Print "Left <right \ n ";
}

If ($ string1 GT $ string2 ){
Print "Left> right \ n ";
}

If ($ string1 le $ string2 ){
Print "Left <right \ n ";
}

If ($ string1 Ge $ string2 ){
Print "Left> right \ n ";
}

11. Character Segmentation-split
@ Array = Split (pattern, string), which divides a Perl string into multiple words in a certain pattern.
#! /Usr/bin/perl
$ STR = "abcdeifg12i34567 ";
@ Array = Split (//, $ Str); split by Space
Foreach (@ array ){
Print $ _, "\ n ";
}

[HTO @ localhost] $./tip. pl
Abcdei
Fg12i
345
6
7

@ Array = Split (/+/, $ line); when there is more than one space between words in a row.

Split when spaces and tabs are mixed
[HTO @ localhost] $ vitip. pl

#! /Usr/bin/perl
$ STR = "abcdeifg12i34567 ";
@ Array = Split (/\ t/, $ Str );
Foreach (@ array ){
Print $ _, "\ n ";
}

[HTO @ localhost] $./tip. pl
Abcdeifg12i
34567

I only divided it into two copies. Why? Because there is only one tab and space at the same time, you must add [].
@ Array = Split (/[\ t]/, $ Str); now it is true to divide by space and Tab

[HTO @ localhost] $./tip. pl
Abcdei
Fg12i

345
6
7

However, there is still a defect. When a tab is connected with a space, the tab is considered as a space-based sub-string or a space is considered as a tab-based sub-string.

$ String = "";
@ Array = Split (//, $ string); # This method cannot be divided into "Zhang", "big", "Hua", "love", "medium ", "country" and other words
Print (join (",", @ array), "\ n ");

12. Demonstrate the reverse order of Chinese characters with English characters (multiple functions are used)
Print "------ begin ----- \ n ";
$ String1 = "China love! Zhang Dahua ";
@ Array = unpack ("C *", $ string1 );
$ Length = $ # array; # The Last subscript of this array
For (; $ length> = 0 ;){
If ($ array [$ length] <= 128 ){
# English or punctuation
Push (@ array2, $ array [$ length]);
$ Length = $ length-1;
}
Else {
# Chinese Characters
Push (@ array2, $ array [$ length-1]);
Push (@ array2, $ array [$ length]);
$ Length = $ length-2;
}
}

$ String2 = pack ("C *", @ array2 );
Print "$ string2 \ n ";
$ Dir = dir;
Print QX/$ DIR /;
@ Name = QW/Zheng Wen Feng KITTY /;
Print join (",", @ name );

13. Character merge operation-join
Use join to define the Perl String Array format symbol (default) must be used with QW.
Syntax: Join ($ string, @ array)
@ Array = QW (onetwothree );
$ Total = "one, two, three ";
@ Array = QW (onetwothree );
$ Total = join (":", @ array );
$ Total = "One: Two: Three ";

14. Match the element string in the array-grep
@ Array = ("one", "on", "in ");
$ COUNT = grep (/on/, @ array );
The query result is assigned to a single variable.
@ Array = ("one", "on", "in ");
@ Result = grep (/on/, @ array );
The query result is assigned to the array.
2
One
On

15. String connection-'. ='
A discussion on how to connect Perl strings.
$ Line = $ line. "456 ";
In this statement, line is calculated twice.
$ Line. = "456 ";
The ',' operator is commonly used for output:
Print "gold", $ V1;
Print $ STR, "\ n ";

The '.' operator is similar to 'and'. '. It is also used to add Perl strings but is usually used only for print while'. 'Can be used to add any Perl string.
Print '12345 everybody '. "helloworld ";
The result is: 12345 everybody comes to helloworld

16. Repeated join operator no.-x
Print "OK" X4;
The result is:
Okokokok
You can perform this calculation once. The Perl string connection can be connected to the integer and character form, and the integer is also processed as the character type, without the % d problem in printf.

17. escape characters in double quotation marks
Symbol Meaning
\ N line feed
\ R press ENTER
\ T Tab
\ F formfeed
\ B Return
\ A bell
\ E escape (Escape Character in ASCII)
\ 007 any octal value (here is, 007 = Bell (Bell ))
\ X7f any decimal value (here is, 007 = bell)
\ CC a controller (CTRL + C)
\ Backslash
\ "Double quotation marks
\ L lower case letters
\ L followed by lowercase characters until \ e
\ U upper case
\ U followed by uppercase characters until \ e
\ Q is prefixed with \ before the non-word character until \ e
\ E end \ L, \ e and \ q

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.