php 正则表达,PHP: POSIX 正则表达式函数 - Manual

mholdgate wrote a very nice quick reference guide in the next page (http://www.php.net/manual/en/function.ereg.php), but I felt it could be improved a little:

________________

^        Start of line

$        End of line

n?        Zero or only one single occurrence of character 'n'

n*        Zero or more occurrences of character 'n'

n+        At least one or more occurrences of character 'n'

n{2}        Exactly two occurrences of 'n'

n{2,}        At least 2 or more occurrences of 'n'

n{2,4}        From 2 to 4 occurrences of 'n'

.        Any single character

()        Parenthesis to group expressions

(.*)        Zero or more occurrences of any single character, ie, anything!

(n|a)        Either 'n' or 'a'

[1-6]        Any single digit in the range between 1 and 6

[c-h]        Any single lower case letter in the range between c and h

[D-M]        Any single upper case letter in the range between D and M

[^a-z]        Any single character EXCEPT any lower case letter between a and z.

Pitfall: the ^ symbol only acts as an EXCEPT rule if it is the

very first character inside a range, and it denies the

entire range including the ^ symbol itself if it appears again

later in the range. Also remember that if it is the first

character in the entire expression, it means "start of line".

In any other place, it is always treated as a regular ^ symbol.

In other words, you cannot deny a word with ^undesired_word

or a group with ^(undesired_phrase).

Read more detailed regex documentation to find out what is

necessary to achieve this.

[_4^a-zA-Z]    Any single character which can be the underscore or the

number 4 or the ^ symbol or any letter, lower or upper case

?, +, * and the {} count parameters can be appended not only to a single character, but also to a group() or a range[].

therefore,

^.{2}[a-z]{1,2}_?[0-9]*([1-6]|[a-f])[^1-9]{2}a+$

would mean:

^.{2}         = A line beginning with any two characters,

[a-z]{1,2}     = followed by either 1 or 2 lower case letters,

_?         = followed by an optional underscore,

[0-9]*         = followed by zero or more digits,

([1-6]|[a-f])     = followed by either a digit between 1 and 6 OR a

lower case letter between a and f,

[^1-9]{2}     = followed by any two characters except digits

between 1 and 9 (0 is possible),

a+$         = followed by at least one or more

occurrences of 'a' at the end of a line.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值