如何用大写字母替换特定字符?

发布于 2024-12-09 15:34:10 字数 271 浏览 1 评论 0原文

考虑下面的字符串

String = "this is for test. i'm new to perl! Please help. can u help? I 希望如此。"

在上面 之后的字符串中。?! 下一个字符应为大写。我怎样才能做到这一点?

我正在逐行读取文本文件,并且需要将修改后的数据写入另一个文件。

我们将非常感谢您的帮助。

Consider the following string

String = "this is for test. i'm new to perl! Please help. can u help? i hope so."

In the above string after . or ? or ! the next character should be in upper case. how can I do that?

I'm reading from text file line by line and I need to write modified data to another file.

your help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

所谓喜欢 2024-12-16 15:34:10

你可以使用正则表达式
试试这个:

my $s = "...";
$s =~ s/([\.\?!]\s*[a-z])/uc($1)/ge; # of course $1 , thanks to plusplus

g-flag 搜索所有匹配项,e-flag 执行 uc 将字母转换为大写

说明:

  • 使用 [.\?!] 搜索标点符号
  • \s* 用于搜索标记和下一个单词的第一个字母和
  • [az] 匹配单个字母(在这种情况下,

上面提到的正则表达式使用这些模式搜索下一个单词的第一个字母,以查找标点符号后跟(可选)空格和并将其替换为 uc 的结果(将匹配结果转换为大写)

例如:

my $s = "this is for test. i'm new to perl! Please help. can u help? i hope so.";
$s =~ s/([\.\?!]\s*[a-z])/uc(&1)/ge;
print $s;

将查找“.i”、“!P”、“.c”和“?i”并替换 then,因此打印结果是:

this is for test. I'm new to perl! Please help. Can u help? I hope so.

you could use a regular expression
try this:

my $s = "...";
$s =~ s/([\.\?!]\s*[a-z])/uc($1)/ge; # of course $1 , thanks to plusplus

the g-flag searches for all matches and the e-flag executes uc to convert the letter to uppercase

Explanation:

  • with [.\?!] you search for your punctuation marks
  • \s* is for whitespaces between the marks and the first letter of your next word and
  • [a-z] matches on a single letter (in this case the first one of the next word

the regular expression mentioned above searches with these patterns for every appearance of a punctuation mark followed by (optional) whitespaces and a letter and replaces it with the result of uc (which converts the match to uppercase).

For example:

my $s = "this is for test. i'm new to perl! Please help. can u help? i hope so.";
$s =~ s/([\.\?!]\s*[a-z])/uc(&1)/ge;
print $s;

will find ". i", "! P", ". c" and "? i" and replaces then, so the printed result is:

this is for test. I'm new to perl! Please help. Can u help? I hope so.
心凉怎暖 2024-12-16 15:34:10

您可以使用替换运算符s///

   $string =~ s/([.?!]\s*\S)/ uc($1) /ge;

You can use the substitution operator s///:

   $string =~ s/([.?!]\s*\S)/ uc($1) /ge;
2024-12-16 15:34:10

这是一个split解决方案:

$str = "this is for test. im new to perl! Please help. can u help? i hope so."; 
say join "", map ucfirst, split /([?!.]\s*)/, $str;

如果您所做的只是打印到一个新文件,则不需要将字符串连接起来。例如

while ($line = <$input>) {
    print $output map ucfirst, split /([?!.]\s*)/, $line;
}

Here's a split solution:

$str = "this is for test. im new to perl! Please help. can u help? i hope so."; 
say join "", map ucfirst, split /([?!.]\s*)/, $str;

If all you are doing is printing to a new file, you don't need to join the string back up. E.g.

while ($line = <$input>) {
    print $output map ucfirst, split /([?!.]\s*)/, $line;
}
℉絮湮 2024-12-16 15:34:10

编辑 - 完全误读了这个问题,以为您只是出于某种原因要求将 i 大写,对于任何混淆表示歉意!

正如到目前为止的答案所述,您可以查看正则表达式和替换运算符 (s///)。不过,没有人提到 \b (单词边界)字符,这对于查找单个 i 可能很有用 - 否则您将不得不继续添加标点符号您找到的字符类匹配的字符([ ... ])。

例如

my $x = "this is for test. i'm new to perl! Please help. can u help? i hope so. ".
       \"i want it to work!\". Dave, Bob, Henry viii and i are friends. foo i bar.";

$x =~ s/\bi\b/I/g;  # or could use the capture () and uc($1) in eugene's answer

给出:

# this is for test. I'm new to perl! Please help. can u help? I hope so. 
# "I want it to work!". Dave, Bob, Henry viii and I are friends. foo I bar.

edit - completely misread the question, thought you were just asking to uppercase the is for some reason, apologies for any confusion!

as the answers so far state, you could look at regular expressions, and the substitution operator (s///). No-one has mentioned the \b (word boundary) character though, which may be useful to find the single is - otherwise you are going to have to keep adding punctuation characters that you find to the character class match (the [ ... ]).

e.g.

my $x = "this is for test. i'm new to perl! Please help. can u help? i hope so. ".
       \"i want it to work!\". Dave, Bob, Henry viii and i are friends. foo i bar.";

$x =~ s/\bi\b/I/g;  # or could use the capture () and uc($1) in eugene's answer

gives:

# this is for test. I'm new to perl! Please help. can u help? I hope so. 
# "I want it to work!". Dave, Bob, Henry viii and I are friends. foo I bar.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文