gpt4 book ai didi

regex - 如何在perl中将字符串与变音符号匹配?

转载 作者:行者123 更新时间:2023-12-02 01:47:40 26 4
gpt4 key购买 nike

例如,在“”Îñţérñåţîöñåļîžåţîöñ”中匹配“Nation”,无需额外模块。在新的 Perl 版本(5.14、5.15 等)中是否可以?

I found an answer! Thanks to tchrist

与 UCA 匹配的正确解决方案(感谢 https://stackoverflow.com/users/471272/tchrist )。

# found start/end offsets for matched utf-substring (without intersections)
use 5.014;
use strict;
use warnings;
use utf8;
use Unicode::Collate;
binmode STDOUT, ':encoding(UTF-8)';
my $str = "Îñţérñåţîöñåļîžåţîöñ" x 2;
my $look = "Nation";
my $Collator = Unicode::Collate->new(
normalization => undef, level => 1
);

my @match = $Collator->match($str, $look);
if (@match) {
my $found = $match[0];
my $f_len = length($found);
say "match result: $found (length is $f_len)";
my $offset = 0;
while ((my $start = index($str, $found, $offset)) != -1) {
my $end = $start + $f_len;
say sprintf("found at: %s,%s", $start, $end);
$offset = $end + 1;
}
}

来自 http://www.perlmonks.org/?node_id=485681 的错误(但有效)解决方案

Magic piece of code is:

    $str = Unicode::Normalize::NFD($str); $str =~ s/\pM//g;

code example:

    use 5.014;
use utf8;
use Unicode::Normalize;

binmode STDOUT, ':encoding(UTF-8)';
my $str = "Îñţérñåţîöñåļîžåţîöñ";
my $look = "Nation";
say "before: $str\n";
$str = NFD($str);
# M is short alias for \p{Mark} (http://perldoc.perl.org/perluniprops.html)
$str =~ s/\pM//og; # remove "marks"
say "after: $str";¬
say "is_match: ", $str =~ /$look/i || 0;

最佳答案

UCA 的正确解决方案(感谢 tchrist):

# found start/end offsets for matched s
use 5.014;
use utf8;
use Unicode::Collate;
binmode STDOUT, ':encoding(UTF-8)';
my $str = "Îñţérñåţîöñåļîžåţîöñ" x 2;
my $look = "Nation";
my $Collator = Unicode::Collate->new(
normalization => undef, level => 1
);

my @match = $Collator->match($str, $look);
say "match ok!" if @match;

附注“假设你可以删除变音符号来获取基本 ASCII 字母的代码是邪恶的、静止的、 splinter 的、脑损伤的、错误的,并且是死刑的理由。”© 基督 Why does modern Perl avoid UTF-8 by default?

关于regex - 如何在perl中将字符串与变音符号匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7429964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com