duanbeng6709 2012-11-30 13:29
浏览 17
已采纳

理解正则表达式

I am tired of being frightened of regular expressions. The topic of this post is limited to PHP implementation of regular expressions, however, any generic regular expression advice would obviously be appreciated (i.e. don't confuse me with scope that is not applicable to PHP).

The following (I believe) will remove any whitespace between numbers. Maybe there is a better way to do so, but I still want to understand what is going on.

$pat="/\b(\d+)\s+(?=\d+\b)/";
$sub="123 345";
$string=preg_replace($pat, "$1", $sub);

Going through the pattern, my interpretation is:

  • \b A word boundary
  • \d+ A subpattern of 1 or more digits
  • \s+ One or more whitespaces
  • (?=\d+\b) Lookahead assertion of one or more digit followed by a word boundary?
  • Putting it all together, search for any word boundary followed by one or more digits and then some whitespace, and then do some sort of lookahead assertion on it, and save the results in $1 so it can replace the pattern?

Questions:

  • Is my above interpretation correct?
  • What is that lookahead assertion all about?
  • What is the purpose of the leading / and trailing /?
  • 写回答

2条回答 默认 最新

  • douzuo0002 2012-11-30 13:35
    关注

    Is my above interpretation correct?

    Yes, your interpretation is correct.

    What is that lookahead assertion all about?

    That lookahead assertion is a way for you to match characters that have a certain pattern in front of them, without actually having to match the pattern.

    So basically, using the regex abcd(?=e) to match the string abcde will give you the match: abcd.

    The reason that this matches is that the string abcde does in fact contain:

    1. An a
    2. Followed by a b
    3. Followed by a c
    4. Followed by a d that has an e after it (this is a single character!)

    It is important to note that after the 4th item it also contains an actual "e" character, which we didn't match.

    On the other hand, trying to match the string against the regex abcd(?=f) will fail, since the sequence:

    "a", followed by "b", followed by "c", followed by "d that has an f in front of it"
    

    is not found.

    What is the purpose of the leading / and trailing /

    Those are delimiters, and are used in PHP to distinguish the pattern part of your string from the modifier part of your string. A delimiter can be any character, although I prefer @ signs myself. Remember that the character you are using as a delimiter needs to be escaped if it is used in your pattern.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向