使用 perl 匹配句子中的句号

发布于 2024-12-20 08:53:01 字数 276 浏览 0 评论 0原文

如何匹配句子中的句号(句号),但我不想匹配浮点数或包含数字的单词?

例如。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铁轨上的流浪者 2024-12-27 08:53:01

使用look around :

$sen =~ s/(?<!\d)\.(?!\d)//g;

这将匹配前面没有数字且后面没有数字的点。

根据OP的评论进行更新,这将删除后面跟着大写字母的点:

#!/usr/bin/perl
use Modern::Perl;
use utf8;

while(<DATA>) {
    chomp;
    s/\.(?=(?:\s*[A-Z])|$)//g;
    # Or, if you want to be unicode compatible
    s/\pP(?=(?:\s*\p{Lu})|$)//g;
    say;
}

__DATA__
I'm going to match full.stop in sentence 3.142
I'm going to match full.Stop in sentence 3.142
I'm going to match full. Stop in sentence 3.142
I'm going to match full.stop in sentence 3.142. End of string.

输出:

I'm going to match full.stop in sentence 3.142
I'm going to match fullStop in sentence 3.142
I'm going to match full Stop in sentence 3.142
I'm going to match full.stop in sentence 3.142 End of string

Use look around :

$sen =~ s/(?<!\d)\.(?!\d)//g;

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:

#!/usr/bin/perl
use Modern::Perl;
use utf8;

while(<DATA>) {
    chomp;
    s/\.(?=(?:\s*[A-Z])|$)//g;
    # Or, if you want to be unicode compatible
    s/\pP(?=(?:\s*\p{Lu})|$)//g;
    say;
}

__DATA__
I'm going to match full.stop in sentence 3.142
I'm going to match full.Stop in sentence 3.142
I'm going to match full. Stop in sentence 3.142
I'm going to match full.stop in sentence 3.142. End of string.

output:

I'm going to match full.stop in sentence 3.142
I'm going to match fullStop in sentence 3.142
I'm going to match full Stop in sentence 3.142
I'm going to match full.stop in sentence 3.142 End of string
离线来电— 2024-12-27 08:53:01

您可以使用 /(\.(\D|$))|\D\./\D 表示非数字字符,$ 表示行尾

You can use /(\.(\D|$))|\D\./. \D means non digit character, and $ means end of the line

丑丑阿 2024-12-27 08:53:01

如果要删除第一个句点(“full.stop”中间的句点),但保留第二个句点(3.142 中的句点),并保留其数字,例如“1”。或“p.1223”,您可以执行以下操作:

$sen =~ s/(\D)\.(\D)/$1$2/g;
print $sen;

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:

$sen =~ s/(\D)\.(\D)/$1$2/g;
print $sen;
遗弃M 2024-12-27 08:53:01

使正则表达式尽可能简单是件好事,因为它们已经很难阅读了。

要匹配一个或多个非数字和空白,然后匹配 '.',然后再匹配一个或多个非数字和非空白:

$sen = "I'm going to match full.stop in sentence 3.142";
print "$1\n" if $sen =~ /([^\d\s]+\.[^\d\s]+)/';

给出:

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:

$sen = "I'm going to match full.stop in sentence 3.142";
print "$1\n" if $sen =~ /([^\d\s]+\.[^\d\s]+)/';

Gives:

full.stop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文