正则表达式在Perl中获取标点符号后单词的第一个字母
谁能告诉我 Perl 中的正则表达式,用于获取点、问号或感叹号后面的单词的 ucfirst 字母...
我的程序逐个字符读取字符串。
要求:
input string : "abcd[.?!]\s*abcd"
output: "Abcd[.?!]\s*Abcd"
我的程序是如下:
#!/usr/bin/perl
use strict;
my $str = <STDIN>;
my $len=length($str);
my $ch;
my $i;
for($i=0;$i<=length($str);$i++)
{
$ch = substr($str,$i,1);
print "$ch";
if($ch =~ 's/([.?!]\s*[a-z])/uc($1)/ge')
{
$i=$i+1;
$ch = substr($str, $i,1);
my $ch = uc($ch);
print "$ch";
}
#elsif($ch eq "?")
#{
# $i=$i+1;
# $ch = substr($str, $i,1);
# my $ch = uc($ch);
# print "$ch";
#}
#elsif($ch eq "!")
#{
# $i=$i+1;
# $ch = substr($str, $i,1);
# my $ch = uc($ch);
# print"$ch";
#}
#elsif($ch eq " ")
#{
# $i=$i+1;
# $ch = substr($str, $i,1);
# my $ch = uc($ch);
# print"$ch";
#}
#else
#{
#print "";
#}
}
print "\n";
Can any body tell me a regular expression in Perl for getting the ucfirst letter of the word comming after a dot,question or exclamation sign...
My program reads string character by character.
Requirement :
input string : "abcd[.?!]\s*abcd"
output: "Abcd[.?!]\s*Abcd"
My program is as follows:
#!/usr/bin/perl
use strict;
my $str = <STDIN>;
my $len=length($str);
my $ch;
my $i;
for($i=0;$i<=length($str);$i++)
{
$ch = substr($str,$i,1);
print "$ch";
if($ch =~ 's/([.?!]\s*[a-z])/uc($1)/ge')
{
$i=$i+1;
$ch = substr($str, $i,1);
my $ch = uc($ch);
print "$ch";
}
#elsif($ch eq "?")
#{
# $i=$i+1;
# $ch = substr($str, $i,1);
# my $ch = uc($ch);
# print "$ch";
#}
#elsif($ch eq "!")
#{
# $i=$i+1;
# $ch = substr($str, $i,1);
# my $ch = uc($ch);
# print"$ch";
#}
#elsif($ch eq " ")
#{
# $i=$i+1;
# $ch = substr($str, $i,1);
# my $ch = uc($ch);
# print"$ch";
#}
#else
#{
#print "";
#}
}
print "\n";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
循环字符串,然后循环匹配,是完全多余的。您的整个程序可以替换为:
我将行开头添加到第一个括号表达式中,尽管您的解释不包括这一点(但您的示例包含)。
Looping over the string, and then looping over the match, is completely redundant. Your entire program can be replaced with this:
I added beginning of line to the first parenthesized expression, although your explanation doesn't include that (but your example does).
通常,
但是如果你一次只读一个字符,
Normally,
But if you only read one character at a time,
您的输出与您的解释不符。在输入中,首字母“a”后面没有句号、问号或感叹号,而是更改为大写。
您可以而且应该通过一次替换来完成这种处理。完全按照您所说的操作:
\K
丢弃与 [.?!] 匹配的字符,只留下匹配字符串中的小写字母。$&
是匹配的字符串。e
标志表示评估uc($&)
。如果您还想将首字母变为大写:
Your output does not match your explanation. In the input, the initial "a" does not follow a period, question mark, or exclamation mark, but was changed to upper case.
You can and should do this sort of processing with a single substitution. To do exactly as you said:
The
\K
discards the character matched by [.?!], leaving only the lower-case letter in the matched string.$&
is the matched string. Thee
flag says to evaluateuc($&)
.If you also want to make an initial letter uppercase:
如果你有 unicode 字符串,你可以使用:
If you have unicode string, you could use: