doumiebiao6827 2017-08-09 20:48
浏览 50
已采纳

(正则表达式)\ b和NOT \ B之间的错误差异

I wanted to test the workings of \B specifically, across different
engines that should all be handled the same.

I needed to do this to feel confident in using this construct.

The regex engines are: Perl, PHP, JS, and Boost.
These engines should ALL produce the exact same result.

But they don't.

In theory, \B should match everywhere \b doesn't.
The list below show the results using the exact same regex on the different engines.

To me, it looks like PHP and JavaScript work as per definition,
but Perl and Boost are slightly different.

If anyone can validate these results and/or shed light on whether any of
this is the result of bugs.
Or, possibly there is a misconception of the definitions of \b and \B
as, for these engines, they absolutely are supposed to be exactly the same.

Thanks for your time!

(the simple code to produce this output is not included, but can be if needed)

 Target      = '   ssssssssssssss '
 Replacement = '<>'

 ==================================================
 PHP 7.03

 \b      = '   <>ssssssssssssss<> '
 \B      = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>'
 (?!\b)  = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>'
 (?<!\b) = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>'
 (?!\B)  = '   <>ssssssssssssss<> '
 (?<!\B) = '   <>ssssssssssssss<> '


 =======================================
 Perl 5.20

 \b      = '   <>ssssssssssssss<> '
 \B      = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s '
 (?!\b)  = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>'
 (?<!\b) = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>'
 (?!\B)  = '   <>ssssssssssssss<> '
 (?<!\B) = '   <>ssssssssssssss<> '

 ========================================
 Boost 1.64

 \b      = '   <>ssssssssssssss<> '
 \B      = ' <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s '
 (?!\b)  = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>'
 (?<!\b) = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>'
 (?!\B)  = '<>   <>ssssssssssssss<> <>'
 (?<!\B) = '<>   <>ssssssssssssss<> <>'

 =====================================
 JavaScript

 \b      = '   <>ssssssssssssss<> '
 \B      = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>'
 (?!\b)  = '<> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>'
 (?!\B)  = '   <>ssssssssssssss<> '
  • 写回答

1条回答 默认 最新

  • dongwen7423 2017-08-09 21:16
    关注

    The bug was fixed in the version of Perl that was released after the one you used.

    $ perl -le'print $^V'
    v5.20.1
    
    $ for re in '\b' '\B' '(?!\b)' '(?<!\b)' '(?!\B)' '(?<!\B)' ; do
    >    perl -e'printf "%-7s = %s
    ", $ARGV[0], "   ssssssssssssss " =~ s/$ARGV[0]/<>/rg' \
    >       "$re"
    > done
    \b      =    <>ssssssssssssss<>
    \B      = <> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s
    (?!\b)  = <> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>
    (?<!\b) = <> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>
    (?!\B)  =    <>ssssssssssssss<>
    (?<!\B) =    <>ssssssssssssss<>
    
    $ perl -le'print $^V'
    v5.22.0
    
    $ for re in '\b' '\B' '(?!\b)' '(?<!\b)' '(?!\B)' '(?<!\B)' ; do
    >    perl -e'printf "%-7s = %s
    ", $ARGV[0], "   ssssssssssssss " =~ s/$ARGV[0]/<>/rg' \
    >       "$re"
    > done
    \b      =    <>ssssssssssssss<>
    \B      = <> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>
    (?!\b)  = <> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>
    (?<!\b) = <> <> <> s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s<>s <>
    (?!\B)  =    <>ssssssssssssss<>
    (?<!\B) =    <>ssssssssssssss<>
    

    The Boost version is not only different; it doesn't appear to be self-consistent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程