Perl language learning notes 9 Regular Expressions for text processing, perl Language Learning

Source: Internet
Author: User

Perl language learning notes 9 Regular Expressions for text processing, perl Language Learning

1. Replacement

S/PATTERN/REPLACE/; # returns the Boolean value of whether the replacement is successful.

You can use capture variables, such as s/(\ w)/$1/

If the matching fails, no processing is performed.

2. delimiters

For delimiters without left or right, repeat them three times, for example, s //, s ###

There are two pairs of delimiters with left and right points. One pair of include mode and one pair of replace strings can be different. For example: s {}{}, s [] {}, s <> []

3. Optional Modifier

/G can be replaced globally, replacing all matched strings, such as: s // g

/S: Make. Match All characters

/I: case-insensitive

4. Bind Operators

$ File_name = ~ S # ^. * # s; # Remove all Unix paths

5. Escape Operators

\ U: Convert all to uppercase, or end with \ E

\ L: Convert all to lowercase, or end with \ E

\ U: The next character is converted to uppercase

\ L: the subsequent qualification characters are converted to lowercase letters

Can be used together, \ u \ L all to lowercase, the first letter is capitalized

The preceding operators can be used in double quotation marks.

6. split string: split

@ Fileds = split/:/, "abs: sdf: fdd"; # Return result list

When two delimiters are connected together, the fields are split out, and the ending part is omitted.

/\ S +/can be blank, such as: split/\ s +/, $ some_input; # All blank spaces are treated as a space, omitting the blank fields at the beginning

7. String concatenation: join

My $ x = join ":",; # The result is $ x = "4: 6: 8: 10 ";

Combined with split, split first and connect with different delimiters.

8. m in the list context //

When m // is used in the context of the list, if the matching succeeds, the list of all captured variables is returned. If the matching fails, an empty list is returned: my ($ first, $ second, $ third) =/(\ s + )/;

The/g modifier can match m // to many places, such:

9. Non-Greedy quantifiers

By default,/Fred. + barney/matches "Fred" first, and then matches all the remaining strings as ". + ", and then". + "one character is spit out until" barney "is matched. This is greedy mode.

In + ,*,{},? Add "?", Changes it to a non-Greedy mode, from less to more matching;

/M: Cross-row mode match. After this parameter is added, the line break in the row will be matched;

10. Update multiple files at a time

$ ^ I: indicates the suffix of the backup file. Before the backup is processed, the file is backed up, read from the backup file, and create a file with the same name as the source file to write new information;

11. Online Editing Using command lines

-P: automatically generates a small program, while loop

-I. bak: Backup File Name

-W: open warning

-E: Tell the program the code behind it.

Fred *. dat: file to be processed



Perl processes text and uses regular expressions for matching

I personally think it is troublesome to use regular expressions. Since it is determined to be deleted twice, I suggest using the following method, which has been tested:

For (my $ I = 2; $ I> 0; $ I --){
My $ offLen = rindex ($ SQL, "where ");
My $ start2 = rindex ($ SQL, "'") + 1;
$ SQL = substr ($ SQL, 0, $ offLen). substr ($ SQL, $ start2 );
}
Print "$ SQL \ n ";

# Print
# Select a1.startdate, concat (hour (starttime), ': 00: 00') as starttime, a1.PtopSector as PtopSector, a1.rt181 _ BSC as BSC, a1.rt181 _ CELL_LAC as CELL_LAC, a1.rt181 _ CELL_CI as CELL_CI, a1.rt181 _ CELL_LAC_ADJ as CELL_LAC_ADJ, a1.rt181 _ CELL_CI_ADJ as CELL_CI_ADJ, a1.rt181 _ blank as 'number of cell switch requests ', a1.rt181 _ NB_ADJ_BSC_INC_HO_ATPT as 'number of cell switch attempts cell pair ', a1.rt181 _ NB_ADJ_BSC_INC_HO_SUCC as' number of successful cell switch times cell pair 'from (select * from tmp. tmp180175781250 where startdate = '2017-11-10 'and starttime = '12: 00: 00') a1 left join (select * from tmp. tmp789791091875) a2 on a2.CELL _ CI = a1.rt181 _ CELL_CI left join (select * from tmp. tmp944702148437) a3 on a3.CELL _ CI = a1.rt181 _ CELL_CI_ADJ

Perl Regular Expressions process text documents

Perl-I-pe's // \ t/G' 1.txt
 

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.