开发者

How can I uc only certain words in some text?

I have some text:

A block of text is a stack of line boxes. In the case of 'left', 'right' and 'center', this property specifies how the inline-level boxes within each line box align with respect to the line box's left and right sides; alignment is not with respect to the viewport. In the case of 'justify', this property specifies that th开发者_运维问答e inline-level boxes are to be made flush with both sides of the line box if possible, by expanding or contracting the contents of inline boxes, else aligned as for the initial value. (See also 'letter-spacing' and 'word-spacing'.)

All the words in the text with "th" should be written in uppercase (using function uc).

Here is my code

@tykeldatud=split(/ /, $string);
$j=@tykeldatud;
for ($i=1;$i<=$j;$i++) {
    ...

What should I write next?


It's just a substitution.

use strict;
use warnings;

my $string = <<EOF;
A block of text is a stack of line boxes.  In the case of 'left',
'right' and 'center', this property specifies how the inline-level
boxes within each line box align with respect to the line box's left
and right sides; alignment is not with respect to the viewport.  In
the case of 'justify', this property specifies that the inline-level
boxes are to be made flush with both sides of the line box if
possible, by expanding or contracting the contents of inline boxes,
else aligned as for the initial value.  (See also 'letter-spacing' and
'word-spacing'.)
EOF

$string =~ s/\b(\S*th\S*)\b/uc $1/ieg;
print $string;


use warnings;
use strict;

my $string = <<EOF;
A block of text is a stack of line boxes.  In the case of 'left',
'right' and 'center', this property specifies how the inline-level
boxes within each line box align with respect to the line box's left
and right sides; alignment is not with respect to the viewport.  In
the case of 'justify', this property specifies that the inline-level
boxes are to be made flush with both sides of the line box if
possible, by expanding or contracting the contents of inline boxes,
else aligned as for the initial value.  (See also 'letter-spacing' and
'word-spacing'.)
EOF

my $string2;
for (split / /, $string) {
    $_ = uc if /th/i;
    $string2 .= "$_ ";
}
print "$string2\n";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜