在Perl(新手需要的简单程序)中,之后。或或下一个字符应为大写
可能重复:在Perl(新手需要的简单程序)中,之后。或或下一个字符应为大写,perl,Perl,可能重复: 考虑以下字符串: String = "this is for test. i'm new to perl! Please help. can u help? i hope so." 在上面的字符串中,在后面。或或下一个字符应为大写。此外,句子(字符串)的第一个字母应为大写。我该怎么做? 我正在逐字读字符串 非常感谢你的帮助 问候,, AmitPerl有一个函数,它可以完全满足您的需要。因此,您只需将字符串拆分为多个部分,首先在每个部分上使用uc,然后再次将它们连接在一起 #!/u
考虑以下字符串:
String = "this is for test. i'm new to perl! Please help. can u help? i hope so."
在上面的字符串中,在后面。或或下一个字符应为大写。此外,句子(字符串)的第一个字母应为大写。我该怎么做?
我正在逐字读字符串
非常感谢你的帮助
问候,,
AmitPerl有一个函数,它可以完全满足您的需要。因此,您只需将字符串拆分为多个部分,首先在每个部分上使用uc
,然后再次将它们连接在一起
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my $string = q[this is for test. i'm new to perl! Please help. can u help? i hope so.];
my @bits = split /([.?!]\s+)/, $string;
$string = join '', map { ucfirst } @bits;
say $string;
Perl有一个函数,可以完全满足您的需要。因此,您只需将字符串拆分为多个部分,首先在每个部分上使用uc
,然后再次将它们连接在一起
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my $string = q[this is for test. i'm new to perl! Please help. can u help? i hope so.];
my @bits = split /([.?!]\s+)/, $string;
$string = join '', map { ucfirst } @bits;
say $string;
基本上,您可以尝试此代码
#!/usr/bin/perl
$String = "this is for test. i'm new to perl! Please help. can u help? i hope so.";
$String =~ s/ ((^\w)|(\.\s\w)|(\?\s\w)|(\!\s\w))/\U$1/xg;
print "$String\n";
(^\w)
:行首(\。\s\w)
:在“.”之后加空格(\?\s\w)
:在“?”之后加空格(\!\s\w)
:在“!”之后以基本方式后跟空格,您可以尝试以下代码
#!/usr/bin/perl
$String = "this is for test. i'm new to perl! Please help. can u help? i hope so.";
$String =~ s/ ((^\w)|(\.\s\w)|(\?\s\w)|(\!\s\w))/\U$1/xg;
print "$String\n";
(^\w)
:行首(\。\s\w)
:在“.”之后加空格(\?\s\w)
:在“?”之后加空格(\!\s\w)
:在“!”之后后跟空格#/usr/bin/perl使用严格;使用File::Slurp;使用文件::Slurp qw(:all);打开(在文件'matter.txt')| |死(“无法打开文件!”);我的$F1=读取_文件('matter.txt')$F1=ucfirst($F1);打印“$F1\n”;我的$F2=~s/([\.\?!]\s*[a-z])/uc(&1)/ge;我的$F3=写入文件('copy\u matter.txt',$F1);写入文件('copy\u matter\u 1.txt',$F2);关闭(在|文件中)|死亡(“无法关闭文件!!”);问题是:为什么“$F2”不起作用/usr/bin/perl使用严格;使用File::Slurp;使用文件::Slurp qw(:all);打开(在文件'matter.txt')| |死(“无法打开文件!”);我的$F1=读取_文件('matter.txt')$F1=ucfirst($F1);打印“$F1\n”;我的$F2=~s/([\.\?!]\s*[a-z])/uc(&1)/ge;我的$F3=写入文件('copy\u matter.txt',$F1);写入文件('copy\u matter\u 1.txt',$F2);关闭(在|文件中)|死亡(“无法关闭文件!!”);问题是:为什么“$F2”不起作用?