[英]Find text enclosed by @ and replace the inside
問題:
在@
包圍的文件中查找文本片段並替換其中的內容
輸入:
@abc@ abc @ABC@
cba @cba CBA@
所需的輸出:
абц abc АБЦ
cba цба ЦБА
我有以下內容:
#!/usr/bin/perl
use strict;
use warnings;
use Encode;
my $output;
open FILE,"<", 'test.txt';
while (<FILE>) {
chomp(my @chars = split(//, $_));
for (@chars) {
my @char;
$_ =~ s/a/chr(0x430)/eg;
$_ =~ s/b/chr(0x431)/eg;
$_ =~ s/c/chr(0x446)/eg;
$_ =~ s/d/chr(0x434)/eg;
$_ =~ s/e/chr(0x435)/eg;
$_ =~ s/A/chr(0x410)/eg;
$_ =~ s/B/chr(0x411)/eg;
$_ =~ s/C/chr(0x426)/eg;
push @char, $_;
$output = join "", @char;
print encode("utf-8",$output);}
print "\n";
}
close FILE;
但我仍然堅持如何進一步處理
預先感謝您的幫助!
Kluther
這是我的解決方案。 (您將修復它,是的。它是原型)
for (my $data = <DATA>){
$data=~s/[@]([\s\w]+)[@]/func($1)/ge;
print $data;
# while($data=~m/[@]([\s\w]+)[@]/g){
# print "marked: ",$1,"\n";
# print "position:", pos();
# }
# print "not marked: ";
}
sub func{
#do your magic here ;)
return "<< @_ >>";
}
__DATA__
@abc@ abc @ABC@ cba @cba CBA@
這里會發生什么?
首先,我讀取數據。 你可以自己做。
for (my $data = <DATA>){...}
接下來,我需要搜索您的模式並將其替換。
我該怎么辦?
使用substition operator: s/pattern/replace/
但是以有趣的形式:
s/pattern/func($1)/ge
關鍵字g
均值全局搜索
關鍵e
均值評估
因此,我認為您需要編寫自己的func
函數;)
使用transliteration operator: tr/listOfSymbolsToBeReplaced/listOfSymbolsThatBePlacedInstead/
可能更好transliteration operator: tr/listOfSymbolsToBeReplaced/listOfSymbolsThatBePlacedInstead/
$output
處理后,請嘗試此操作。
$output =~ s/\@//g;
my @split_output = split(//, $output);
$output = "";
my $len = scalar(@split_output) ;
while ($len--) {
$output .= shift(@split_output);
}
print $output;
可以使用單個正則表達式完成,而無需拆分字符串:
use strict;
use warnings;
use Encode;
my %chars = (
a => chr(0x430),
b => chr(0x431),
c => chr(0x446),
d => chr(0x434),
e => chr(0x435),
A => chr(0x410),
B => chr(0x411),
C => chr(0x426),
);
my $regex = '(' . join ('|', keys %chars) . ')';
while (<DATA>) {
1 while ($_ =~ s|\@(?!\s)[^@]*?\K$regex(?=[^@]*(?!\s)\@)|$chars{$1}|eg);
print encode("utf-8",$_);
}
由於匹配項的重疊性質,確實需要重復運行正則表達式。
在對算法進行最少更改的情況下,您需要跟蹤自己是否在@
標記內。 所以添加這樣的東西
my $bConvert = 0;
chomp(my @chars = split(//, $_));
for (@chars) {
my $char = $_;
if (/@/) {
$bConvert = ($bConvert + 1) % 2;
next;
}
elsif ($bConvert) {
$char =~ s/a/chr(0x430)/eg;
$char =~ s/b/chr(0x431)/eg;
$char =~ s/c/chr(0x446)/eg;
$char =~ s/d/chr(0x434)/eg;
$char =~ s/e/chr(0x435)/eg;
$char =~ s/A/chr(0x410)/eg;
$char =~ s/B/chr(0x411)/eg;
$char =~ s/C/chr(0x426)/eg;
}
print encode("utf-8",$char);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.