正则表达式和字符大小写

发布于 2024-09-14 00:21:28 字数 317 浏览 2 评论 0原文

好吧,我得到了一个相当简单的(至少看起来很简单)。我有一个多行字符串,我只是在尝试用其他东西替换不同的单词。让我告诉你...

#!/usr/bin/perl -w
use strict;

$_ = "That is my coat.\nCoats are very expensive.";
s/coat/Hat/igm;
print;

输出将是
那是我的帽子
帽子非常昂贵...

第一行的“hat”不应该大写。有什么技巧可以让大小写符合英文的书写方式吗?谢谢 :)

Okay, I got a rather simple one (at least seems simple). I have a multi lined string and I am just playing around with replacing different words with something else. Let me show you...

#!/usr/bin/perl -w
use strict;

$_ = "That is my coat.\nCoats are very expensive.";
s/coat/Hat/igm;
print;

The output would be
That is my Hat
Hats are very expensive...

The "hat" on the first line shouldn't be capitalized. Are there any tricks that can make the casing compliant with how english is written? Thanks :)

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

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

发布评论

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

评论(4

稚气少女 2024-09-21 00:21:28

您可以使用 e 修饰符到 s/// 来实现此目的:

s/(coat)/ucfirst($1) eq $1 ? 'Hat' : 'hat'/igme;

You can use the e modifier to s/// to do the trick:

s/(coat)/ucfirst($1) eq $1 ? 'Hat' : 'hat'/igme;
全部不再 2024-09-21 00:21:28

其一,您应该使用 \b (单词边界)来仅匹配整个单词。例如,s/hat/coat/ 会将 That 更改为 Tcoat,而无需前导 \b。现在回答你的问题。使用标志 /e 您可以在正则表达式的替换部分中使用 Perl 代码。因此,您可以编写一个 Perl 函数来检查匹配的大小写,然后正确设置替换的

my $s = "That is my coat.\nCoats are very expensive.";
$s =~ s/(\bcoat)/&same_case($1, "hat")/igme;
print $s, "\n";

sub same_case {
        my ($match, $replacement) = @_;

        # if match starts with uppercase character, apply ucfirst to replacement
        if($match =~ /^[A-Z]/) {
                return ucfirst($replacement);
        }
        else {
                return $replacement;
        }
}

大小写:

That is my hat.
Hats are very expensive.

For one, you should use \b (word boundary) to match only the whole word. For example s/hat/coat/ would change That to Tcoat without leading \b. Now for your question. With the flag /e you can use Perl code in the replacement part of the regex. So you can write a Perl function that checks the case of the match and then set the case of the replacement properly:

my $s = "That is my coat.\nCoats are very expensive.";
$s =~ s/(\bcoat)/&same_case($1, "hat")/igme;
print $s, "\n";

sub same_case {
        my ($match, $replacement) = @_;

        # if match starts with uppercase character, apply ucfirst to replacement
        if($match =~ /^[A-Z]/) {
                return ucfirst($replacement);
        }
        else {
                return $replacement;
        }
}

Prints:

That is my hat.
Hats are very expensive.
樱娆 2024-09-21 00:21:28

这可能会解决您的问题:


#!/usr/bin/perl -w

use strict;

sub smartSubstitute {
    my $target = shift;
    my $pattern = shift;
    my $replacement = shift;

    $pattern = ucfirst $pattern;
    $replacement = ucfirst $replacement;

    $target =~ s/$pattern/$replacement/gm;

    $pattern = lcfirst $pattern;
    $replacement = lcfirst $replacement;

    $target =~ s/$pattern/$replacement/gm;

    return $target;
}

my $x = "That is my coat.\nCoats are very expansive.";
my $y = smartSubstitute($x, "coat", "Hat");
print $y, "\n";

This may solve your problem:


#!/usr/bin/perl -w

use strict;

sub smartSubstitute {
    my $target = shift;
    my $pattern = shift;
    my $replacement = shift;

    $pattern = ucfirst $pattern;
    $replacement = ucfirst $replacement;

    $target =~ s/$pattern/$replacement/gm;

    $pattern = lcfirst $pattern;
    $replacement = lcfirst $replacement;

    $target =~ s/$pattern/$replacement/gm;

    return $target;
}

my $x = "That is my coat.\nCoats are very expansive.";
my $y = smartSubstitute($x, "coat", "Hat");
print $y, "\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文