Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何进行条件(“如果存在”逻辑)搜索;在Perl中替换?_Regex_Perl_Search_Replace_Conditional - Fatal编程技术网

Regex 如何进行条件(“如果存在”逻辑)搜索;在Perl中替换?

Regex 如何进行条件(“如果存在”逻辑)搜索;在Perl中替换?,regex,perl,search,replace,conditional,Regex,Perl,Search,Replace,Conditional,在我的Perl脚本中,我想使用正则表达式进行条件搜索和替换:找到某个模式,如果该模式存在于散列中,则用其他内容替换它 例如,我想搜索“pattern1”和“pattern2”的组合,如果后者存在于散列中,则用“pattern1”和“replacement”替换该组合。我尝试了下面的方法,但根本没用 $_ =~ s/(pattern1)(pattern2)/$1replacement/gs if exists $my_hash{$2}; 我也尝试过这样的东西: $_ =~ s/(pattern1

在我的Perl脚本中,我想使用正则表达式进行条件搜索和替换:找到某个模式,如果该模式存在于散列中,则用其他内容替换它

例如,我想搜索“pattern1”和“pattern2”的组合,如果后者存在于散列中,则用“pattern1”和“replacement”替换该组合。我尝试了下面的方法,但根本没用

$_ =~ s/(pattern1)(pattern2)/$1replacement/gs if exists $my_hash{$2};
我也尝试过这样的东西:

$_ =~ s/(pattern1)(pattern2) && exists $my_hash{$2}/$1replacement/gs;
也什么都不做,好像找不到匹配项

有人能帮我解决这个正则表达式问题吗?Thx~

有什么不对(比如不工作时)

如果
$patt1
作为散列中的一个键存在,则继续使用
$patt1$patt2
替换
$patt1$replacement
。当然,如果在
$text
中找到
$patt1$patt2
,否则什么也不会发生。第一个代码段是循环的,而第二个代码段根本不能这样工作

如果您首先想要
$patt1$patt2
,还需要散列键,那么您似乎必须放慢速度

if ($str =~ /$patt11$patt2/ && exists $h{$patt2}) {
     $str =~ s/$patt1$patt2/$patt1$replacement/gs;
}
如果这就是你想要的,那么它真的很简单:你需要两个不相关的条件,无论你以何种方式改变它。无法合并它们,因为它是圆形的

从结果的角度来看,这些都是相同的。如果任一条件失败,则无论检查顺序如何,都不会发生任何情况


请注意,或者你不必走得太慢,请参阅的帖子。

我会用另一种方式。看起来您有一个“搜索这个,替换那个”散列

因此:


如果您想省略与模式不匹配的行,可以粘贴
&&print在正则表达式之后

它怎么不起作用,错误?什么都不做?等等。。。所以patt2应该存在于某个散列中,好吧;但是patt1和patt2的组合应该存在。。。用绳子?这是正确的吗?@123没有错误;它什么也不做。@zdim好吧,pattern2确实存在于散列中。pattern1与哈希无关;我使用这种“组合”只是为了说明模式2应该在特定的上下文中匹配。。。然后寻找patt1-patt2组合,如果存在,则用patt1-else替换?这就是你想要的吗?我发布了一个快速的答案,请看并让我知道。谢谢你的回复,但这不是我想要的。我真正想要的是:首先,搜索$patt1$patt2;然后,如果找到匹配项并且哈希中存在$patt2,则将其替换为$patt1$replacement。新添加的代码也不起作用。至于原因,我的意图来自我正在尝试做的事情:给定一个文本文件,我想找到所有DFF并用一个新名称替换它们。这里“Dff”的格式类似于“reg foo1”或“reg[3:0]foo2”,但“reg”之后的内容可能是也可能不是Dff。只有当Dff哈希中存在“reg foo3”和“foo3”时,才将foo3视为Dff。文件中可能有许多DFF;我需要找到并替换它们。所以你有“要替换的注册文本”,但只有当“要替换的文本”和“要替换的注册文本”都是散列中的键时,才能替换它?他们两个?顺便说一句,什么是“Dff”-你对这些你工作的东西的名字?什么是“Dff散列”,简单地说就是一个散列,它包含要操作的字符串中的键。这实际上使它完全不同。如果您希望更新问题(因为这些注释中的解释改变了问题),我将删除这个毫无意义的“答案”。如果你想要有用的想法,我建议你这样做。索布里克给出的答案正是我想要的。谢谢;-)我仍然无法完全理解它,但它很漂亮:)它相当简单-您可以使用alternation创建一个(可能很长)正则表达式匹配表达式。该替换“捕获”了$2,然后在替换的右侧使用。但是,在可能的子字符串周围有一些陷阱,这意味着您有时必须按长度对键进行排序。是的,您的代码很好!!说实话,我确实花了一些时间试图弄清楚“map”、“qr”和“compile”的用法,但失败了。现在我只是“按原样”使用您的代码,而不知道它实际上意味着什么,并且它在我的代码中真正起作用!多谢各位~
if ($str =~ /$patt11$patt2/ && exists $h{$patt2}) {
     $str =~ s/$patt1$patt2/$patt1$replacement/gs;
}
#!/usr/bin/env perl
use strict;
use warnings;

#our 'mappings'. 
#note - there can be gotchas here with substrings
#so make sure you anchor patterns or sort, so 
#you get the right 'substring' match occuring. 

my %replace = (
    "this phrase" => "that thing",
    "cabbage"     => "carrot"
);

#stick the keys together into an alternation regex. 
#quotemeta means regex special characters will be escaped. 
#you can remove that, if you want to use regex in your replace keys.     
my $search = join( "|", map {quotemeta} keys %replace );
#compile it - note \b is a zero width 'word break' 
#so it will only match whole words, not substrings. 
$search = qr/\b($search)\b/;

#iterate the special DATA filehandle - for illustration and a runnable example. 
#you probably want <> instead for 'real world' use. 
while (<DATA>) {
    #apply regex match and replace
    s/(XX) ($search)/$1 $replace{$2}/g;
    #print current line. 
    print;
}

##inlined data filehandle for testing. 
__DATA__
XX this phrase cabbage
XX cabbage carrot cabbage this phrase XX this phrase
XX no words here
and this shouldn't cabbage match this phrase at all
XX that thing cabbage
XX carrot carrot cabbage this phrase XX that thing
XX no words here
and this shouldn't cabbage match this phrase at all