Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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
Python 替换圆括号之间的字符串_Python_Regex_Bash_Perl_Shell - Fatal编程技术网

Python 替换圆括号之间的字符串

Python 替换圆括号之间的字符串,python,regex,bash,perl,shell,Python,Regex,Bash,Perl,Shell,我有一个表达式如下所示: when v(a.b.c.d.e.f.g) = v(m.n.o.x.y.z) 我需要最终输出为: when v(a.b^c^d^e^f:g) = v(m.n^o^x^y:z) 简单地说,从v()之间的每个表达式中第二次出现“”开始,如何将每个“”替换为“^”?两个v()表达式出现在同一行上 我尝试了以下方法: setenv test "when v(a.b.c.d.e.f.g) = v(m.n.o.x.y.z)" echo $test | awk -F"(" '{

我有一个表达式如下所示:

when v(a.b.c.d.e.f.g) = v(m.n.o.x.y.z)
我需要最终输出为:

when v(a.b^c^d^e^f:g) = v(m.n^o^x^y:z)
简单地说,从
v()之间的每个表达式中第二次出现“
”开始,如何将每个“
”替换为“
^
v()
之间的每个表达式中,将最后一个“
”替换为“
”时,代码>?两个
v()
表达式出现在同一行上

我尝试了以下方法:

setenv test "when v(a.b.c.d.e.f.g) = v(m.n.o.x.y.z)"

echo $test | awk -F"(" '{for(i=2;i<=NF;i++){if($i~/\)/){sub(").*","",$i)};print $i}}' | \
    sed -E 's/\./\^/g2' | sed 's/\(.*\)\^/\1\:/'
我需要知道在那之后我如何用上面修改过的文本替换原始语句,以便if最终看起来像这样:

when v(a.b^c^d^e^f:g) = v(m.n^o^x^y:z)
我尝试了以下方法

$ENV{"str"} = "when v(a.b.c.d.e.f.g) = v(m.n.o.x.y.z)";

$str =~ s{(\w(\w\.)(.*?)\.(\w))}{
    my ( $first, $temp, $last) = ( $1, $2, $3 );
    $temp =~ s/\./^/g;
    $first . $temp . ':' . $last
}ge;

system( 'echo $str' );
但它不起作用=(


有什么想法吗?

在评论中,您说字符串应该在一个文件中。 下面是一个例子,说明了这种情况:

use strict;
use warnings;

my $file_name = 'test.txt';

open ( my $fh, '<', $file_name ) or die "Could not open file '$file_name': $!";

my $str = do { local $/; <$fh> };
close $fh;

$str =~ s{(\w\(\w+\.)(.*?)\.(\w+\))}{
  my ( $first, $temp, $last) = ( $1, $2, $3);
  $temp =~ s/\./^/g;
  $first . $temp . ':' . $last}ge;

print $str;
使用严格;
使用警告;
我的$file_name='test.txt';

打开(my$fh,'到目前为止您尝试了什么?请在您的问题中包含一个示例,以演示您编写的代码存在的问题。在Perl中,您可以尝试例如:
$str=~s{(\w\(\w\.)(.*?)(\w\)}{my($first,$temp,$last)=($1,$2,$3);$temp=~s/^/g;$first.$temp.':'.$last}ge
我尝试了以下方法:$ENV{“str”}=“when v(a.b.c.d.e.f.g)=v(m.n.o.x.y.z)”;$str=~s{(\w(\w\)(.*?)(\w)){my($first,$temp,$last)=($1,$2,$3);$temp=~s/^/g;$first.$temp.:'.$last}ge;系统('echo$str');但不起作用=(@SarahMohamed您必须首先声明和定义字符串,然后打印它。无需在Perl中使用
echo
。如果字符串应该在环境变量中,如您所示,您必须修改该变量,而不是
$str
,或者,首先将环境变量复制到
$str
中你还忘了一些反斜杠regex@HåkonHægland字符串不应位于环境变量中,而应位于文件中。我是perl新手,您能否帮助解释我应该做什么,以便通过提供包含字符串的文件来实现这一点?假设test.txt包含以下行:\n.meas tran trst_write_1当v(xsram.xcore\u 0right\u b0.xcore\u d0\u bl0\u 3\u wl0\u 3.xcore\u bl0\u 1\u wl0\u 1.mn11.g)='0.2*supply\u mem'td='4*tp'上升=1\n.表示当v(xsram.xxlgio\u right.xgio\u bit2.dp.xprech\u wd.m0\u 1.d)'giov(xsu.xxlgio.xmu.dp.xl\u 1.dd)时,传输1b\u 0000-0.010'td='trst_write_1a_0000'rise=1\n当我使用您建议的perl脚本时,不会进行替换,并且打印的行与输入文件完全相同。您好,Sarah。是的,但是现在
v
的参数包含的字母与您原来的问题中的一样多。例如
v(xsram..
这里的
xsram
是五个字母,而不是您原来的示例中的一个字母。我已经更新了我的答案以处理您的新规范。我们现在使用
\w+
而不是正则表达式中的
\w
use strict;
use warnings;

my $file_name = 'test.txt';

open ( my $fh, '<', $file_name ) or die "Could not open file '$file_name': $!";

my $str = do { local $/; <$fh> };
close $fh;

$str =~ s{(\w\(\w+\.)(.*?)\.(\w+\))}{
  my ( $first, $temp, $last) = ( $1, $2, $3);
  $temp =~ s/\./^/g;
  $first . $temp . ':' . $last}ge;

print $str;