简体   繁体   English

如何在PHP中制作正则表达式?

[英]How to make regular expressions in PHP?

How to make regular expression in PHP? 如何在PHP中进行正则表达式? Can anyone guide me to the basic rules to make regular expressions? 谁能指导我做正则表达式的基本规则?

http://php.net/manual/en/function.preg-match.php http://php.net/manual/zh/function.preg-match.php

http://php.net/manual/en/function.preg-replace.php http://php.net/manual/zh/function.preg-replace.php

php.net in general has the entire documentation for the php language. php.net通常包含php语言的完整文档。 It's probably one of the more valuable resources available. 它可能是可用的更有价值的资源之一。

You will get basic details of expression content here 您将在此处获得表达式内容的基本详细信息

Table 1. Common regex operators 表1.常见的正则表达式运算符

  1. . (period) Match any single character (句点)匹配任何单个字符
  2. ^ (caret) Match the empty string that occurs at the beginning of a line or string ^(脱字符号)匹配出现在行或字符串开头的空字符串
  3. $ (dollar sign) Match the empty string that occurs at the end of a line $(美元符号)匹配行尾出现的空字符串
  4. A Match an uppercase letter A A匹配大写字母A
  5. a Match a lowercase letter a 匹配小写字母a
  6. \\d Match any single digit \\ d匹配任意一位数字
  7. \\D Match any single nondigit character \\ D匹配任何单个非数字字符
  8. \\w Match any single alphanumeric character; \\ w匹配任何单个字母数字字符; a synonym is [:alnum:] 同义词是[:alnum:]
  9. [AE] Match any of uppercase A, B, C, D, or E [AE]匹配大写A,B,C,D或E
  10. [^AE] Match any character except uppercase A, B, C, D, or E [^ AE]匹配除大写字母A,B,C,D或E外的任何字符
  11. X? X? Match none or one capital letter X 不匹配一个字母或大写字母X
  12. X* Match zero or more capital Xes X *匹配零个或多个大写的Xes
  13. X+ Match one or more capital Xes X +匹配一个或多个大写X
  14. X{n} Match exactly n capital Xes X {n}精确匹配n个大写Xes
  15. X{n,m} Match at least n and no more than m capital Xes; X {n,m}匹配至少n个且不超过m个大写Xes; if you omit m, the expression tries to match at least nXes 如果省略m,则表达式尝试匹配至少nXes个
  16. (abc|def)+ Match a sequence of at least one abc and def;abc and def would match (abc | def)+匹配至少一个abc和def的序列; abc和def将匹配

for more details use following link Mastering regular expressions in PHP 有关更多详细信息,请使用以下链接掌握PHP中的正则表达式

A good start would be the PHP manual for regular expressions: 一个很好的开始是用于正则表达式的PHP手册:

Regular Expressions (Perl-Compatible) 正则表达式(与Perl兼容)

There are three major use cases for regular expressions: 正则表达式有三种主要用例:

查阅本正则表达式教程及其PHP部分,以获取有关如何在PHP中使用正则表达式的详细信息。

请参阅PHP的PCRE函数书 ,特别是有关PCRE模式语法的章节。

Here are a couple tutorials on using regular expressions in PHP that will get you started: 这里有一些关于在PHP中使用正则表达式的教程,可以帮助您入门:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM