regex - 仅在 perl 正则表达式中匹配最后一次出现
问题描述
/1.1/s/1/-/g
我正在做学校作业以参考实现 sed 命令。我得到这个字符串来匹配“/1/-/”。我有实验
$str =~ m{/[^/]*/[^/]*/}g;
但结果是/1.1/s/。我怎样才能只得到“/1/-/”有人可以帮我吗?
解决方案
利用
/[^/]*/[^/]*/(?=[^/]*$)
见证明。
解释
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
[^/]* any character except: '/' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
[^/]* any character except: '/' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
[^/]* any character except: '/' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
推荐阅读
- jquery - 如何将 Html.TextBox 值与 Controller 和 Html.Action 一起使用?
- php - 在 MySQL 中使用 PHP 进行搜索
- c# - 如何在 Unity3D 中创建类似 Snake vs Block 的运动
- postgresql - 在 Postgresql 中创建分区表
- php - Laravel 5.1 中返回函数和调用函数的区别
- python - ValueError: int() 以 10 为底的无效文字:'11,440'
- javascript - 如何获得什么都不做的链接
- javascript - 教代码数学运算
- css - UI Kit 导航栏和按钮中的垂直对齐错误
- php - 为什么我可以在 PHP 中访问深度克隆类中的私有属性?