使用 perl 匹配句子中的句号
如何匹配句子中的句号(句号),但我不想匹配浮点数或包含数字的单词?
例如。
$sen = "I'm going to match full.stop in sentence 3.142";
if ($sen =~ (s/\.//)) {
print $1;
}
输出:
fullstop
在这个例子中,我只想匹配单词或字母数字单词而不是数字。
how can I match full stop (period) in a sentence, but I don't want to match floating numbers or words that contain numbers?
eg.
$sen = "I'm going to match full.stop in sentence 3.142";
if ($sen =~ (s/\.//)) {
print $1;
}
output:
fullstop
In this example I only want to match words or alphanumeric words not numbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用look around :
这将匹配前面没有数字且后面没有数字的点。
根据OP的评论进行更新,这将删除后面跟着大写字母的点:
输出:
Use look around :
This will match a dot not preceded by a digit and not followed by a digit.
Updated according to OP's comment, this will remove dots that are followed by capital letter:
output:
您可以使用
/(\.(\D|$))|\D\./
。\D
表示非数字字符,$
表示行尾You can use
/(\.(\D|$))|\D\./
.\D
means non digit character, and$
means end of the line如果要删除第一个句点(“full.stop”中间的句点),但保留第二个句点(3.142 中的句点),并保留其数字,例如“1”。或“p.1223”,您可以执行以下操作:
If you want to delete the first period (the one in the middle of "full.stop"), but leave the second one (the one in 3.142) intact, and also keep it in numbers such as "1." or "p.1223" you can do the following:
使正则表达式尽可能简单是件好事,因为它们已经很难阅读了。
要匹配一个或多个非数字和空白,然后匹配
'.'
,然后再匹配一个或多个非数字和非空白:给出:
full.stop
It's nice to keep reg-exes as simple as you can, because they're already hard to read.
To match one or more not-digit-and-whitespace then the
'.'
, then again one or more not-digit-and-not-whitespace:Gives:
full.stop