regex - 在 perl 中,当它之前至少有三个单词时匹配一个点
问题描述
我正在使用(?<=(?:(?:\w|,|'){1,20} ){2}(?:\w|,|'){1,20} ?)\.
但它没有按预期工作:
use v5.35.2;
use warnings;
use strict;
my $str = shift // q{If you have to go. you go. That's no problem.};
my $regex = qr/(?<=(?:(?:\w|,|'){1,20} ){2}(?:\w|,|'){1,20} ?)\./;
my @all_parts = split $regex, $str;
say for@all_parts;
它应该打印出来If you have to go
并 you go. That's no problem
有没有更简单的方法来实现这一点?
解决方案
#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say/;
my $str = shift // q{If you have to go. you go. That's no problem.};
my $regex = qr/(?:\b[\w,']+\s*){3}\K\./;
my @all_parts = split $regex, $str;
say for @all_parts;
像你想要的那样分裂。使用\K
在实际匹配期间丢弃所有内容是关键位。(可能会对 RE 进行一些调整,以更好地说明您在示例字符串中未提供的边缘情况)。
推荐阅读
- apache-nifi - 如何监控 NIFI 流文件存储和内容存储阈值
- ios - 呈现的 UIViewController 关闭后刷新父 UIViewController
- c# - 了解新的可为空引用类型警告
- c++ - 是否有必要为 C++ 函数中定义的字符串变量释放内存?
- spring-boot - 使用 Spring Boot 时使用 JaCoCo 代码覆盖率构建 Gradle 多项目失败
- python - 在 python 和 Matlab 之间通过 TCP 套接字传输数据
- javascript - 如何将 jQuery 函数(监视 css 类)转换为 Vue 3 中的可用代码?
- swift - SwiftUI 视图转换匹配分段选择器
- python - 打印熊猫数据框时如何在行之间添加下划线分隔符?
- reactjs - 如何渲染属性位于嵌套对象数组中的元素数组?