我怎么能只在某些文字中使用某些单词?

时间:2011-05-10 17:45:12

标签: perl

我有一些文字:

  

文本块是一堆线框。在“left”,“right”和“center”的情况下,此属性指定每个行框中的内联级框如何相对于行框的左侧和右侧对齐;对齐不是关于视口。在'justify'的情况下,此属性指定如果可能,通过扩展或收缩内联框的内容使内联级框与行框的两边齐平,否则与初始值对齐。 (另请参阅'字母间距'和'字间距'。)

带有“th”的文本中的所有单词都应该用大写字母写成(使用函数uc)。

这是我的代码

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

接下来我应该写什么?

2 个答案:

答案 0 :(得分:3)

这只是一种替代。

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;

答案 1 :(得分:2)

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";