Home  >  Article  >  Backend Development  >  PHP regular expression combat: matching punctuation marks

PHP regular expression combat: matching punctuation marks

PHPz
PHPzOriginal
2023-06-23 08:03:072329browse

In PHP programming, regular expressions are a very commonly used tool that can perform fast and effective matching and replacement operations on strings. Punctuation marks are common string elements that can bring a lot of rich semantics to text. This article will introduce how to use regular expressions to match punctuation marks for more intelligent text processing.

1. Types of punctuation marks

In regular expressions, punctuation marks are not a specific concept or type. Instead, it refers to various common special characters, such as commas, periods, exclamation points, question marks, semicolons, etc. These punctuation marks are widely used in various fields such as linguistics, literature, and publishing.

2. Methods of matching punctuation marks

In regular expressions, you can match various punctuation marks by using special characters or metacharacters. The following are some common punctuation mark matching methods:

  1. Match general punctuation marks

General punctuation marks refer to commas, periods, exclamation marks, question marks, semicolons, Common symbols such as colons can be matched using square brackets []:

preg_match_all('/[,.?!;:]/', $text, $matches);
// 匹配逗号、句号、感叹号、问号、分号、冒号

In the above code, use the preg_match_all function and the regular expression '/[,.?!;:]/' to match all Comma, period, exclamation mark, question mark, semicolon, colon. The square brackets [] represent a character set containing multiple characters. In regular expressions, the period represents a wildcard character for any character and needs to be escaped as .

  1. Matching ellipses

The ellipsis is a symbol composed of three consecutive periods. You can use the regular expression '...' to match them:

preg_match_all('/…/', $text, $matches);
// 匹配省略号

In the above code, use the preg_match_all function and the regular expression '/.../' to match all ellipses. The period in the ellipsis needs to be escaped as .

  1. Match double quotes and single quotes

In PHP programs, double quotes and single quotes are often used to define strings, and these two quotes are also the same Common punctuation marks. You can use square brackets [] to match them:

preg_match_all('/["']/', $text, $matches);
// 匹配双引号和单引号

In the above code, use the preg_match_all function and the regular expression '/["']/' to match double quotes and single quotes. Since the regular expression itself It is also defined using single or double quotes, so it needs to be escaped as ' or ".

3. Case Analysis

In order to better understand the method of matching punctuation marks, a practical case will be demonstrated below: counting the number of times various punctuation marks are used in a text.

First, we can use PHP's built-in file_get_contents function to read a text file into a string variable, and then use the preg_match_all function to match all punctuation marks and save them into an array:

$text = file_get_contents('article.txt');
preg_match_all('/[,.?!;:]/', $text, $matches);
$counts = array_count_values($matches[0]);
print_r($counts);

In the above code, we read a piece of text from a text file and use the regular expression '/[,.?!;:]/' to match all punctuation marks in it. Then, use the PHP built-in function array_count_values ​​to count the number of occurrences of various punctuation marks. Finally, use the print_r function to output the statistical results.

If the text file article.txt contains the following content:

How are you? I'm fine, thank you. What about you? My name's Mike. Nice to meet you!

Then the output result of the above code will be:

Array
(
    [,] => 2
    [.] => 3
    [?] => 2
)

This means that 2 commas, 3 periods, and 2 question marks are used in the above text.

4. Summary

This article introduces the method of using regular expressions to match various punctuation marks, including general punctuation marks, ellipses, double quotes and single quotes, etc. Through the demonstration of actual cases, we can see that using regular expressions can easily and efficiently process various text data, which brings a lot of convenience to program development. In actual development, we can choose the appropriate method based on actual needs and give full play to the power of regular expressions.

The above is the detailed content of PHP regular expression combat: matching punctuation marks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn