regex - 为什么 "\w"不匹配 Perl 正则表达式中的 Unicode 单词字符(例如 "ğ,İ,ş,ç,ö,ü")?

标签 regex perl unicode

为什么 "\w"不匹配 Perl 正则表达式中的 Unicode 单词字符(例如,"ğ,İ,ş,ç,ö,ü")?

我试图在正则表达式中包含这些字符 m{\w+}g .但是,它不匹配 "ğ,İ,ş,ç,ö,ü"。

我怎样才能使这项工作?

use strict;
use warnings;
use v5.12;
use utf8;

open(MYINPUTFILE, "< $ARGV[0]");

my @strings;
my $delimiter;
my $extensions;
my $id;

while(<MYINPUTFILE>)
{
    my($line) = $_;
    chomp($line);
    print $line."\n";
    unshift(@strings,$line =~ /\w+/g);
    $delimiter = /[._\s]/;
    $extensions = /pdf$|doc$|docx$/;
    $id = /^200|^201/;
}

foreach(@strings){
    print $_."\n";
}

输入文件如下:

Çidem_Şener
Hüsnü Tağlip
...



输出如下:
H�

sn�

Ta�

lip

�

idem_�

ener

在代码中,我尝试读取文件并获取数组中的每个字符串。 (分隔符可以是 _.\s )。

最佳答案

确保 Perl 将数据视为 UTF-8。

例如如果它嵌入在脚本本身中:

#!/usr/bin/perl

use strict;
use warnings; 
use v5.12;
use utf8;   # States that the Perl program itself is saved using utf8 encoding

say "matched" if "ğİşçöü" =~ /^\w+$/;

输出匹配。如果我删除 use utf8;行,它没有。

关于regex - 为什么 "\w"不匹配 Perl 正则表达式中的 Unicode 单词字符(例如 "ğ,İ,ş,ç,ö,ü")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9725037/

相关文章:

javascript - Javascript 中的正则表达式 : replace minus with comma+minus when condition is met

javascript - 正则表达式删除字符后的所有内容,包括该字符

perl - 为散列中的 * 字符赋值

regex - 在perl中使用正则表达式检索两个字符串定界符之间的字符串

jquery - 不区分重音的正则表达式

JavaCC 和 Unicode 问题。为什么\u696d属于 "\u4e00"-"\u9fff"范围,但在JavaCC中无法管理

PHP 正则表达式 - 将字符串附加到以 "#' 字符开头的单词

perl - 使用 Perl UserAgent 的 HTTPS 请求

unicode - Unicode 中汉字的完整范围是多少?

regex - 如何删除所有不以某些字符开头的行?