替换给定字符串和换行符之间的字符串

时间:2021-08-10 22:06:49

I have a file which includes only once search_for_me=12.21/13.31/14 followed by a line break. I wish to replace 12.21/13.31/14 with 21.12/44.22/44. How can this be accomplished?

我有一个文件,其中只包含一次search_for_me = 12.21 / 13.31 / 14,然后是换行符。我希望以21.12 / 44.22 / 44取代12.21 / 13.31 / 14。如何实现这一目标?

<?php
/*
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string)."\n\n\n";
//April1,2003
*/
$string = file_get_contents('test.conf');

$string= <<<EOT
bla bla bla

search_for_me=12.21/13.31/14

bla bla bla
EOT;

echo $string."\n\n";

$replace = "21.12/44.22/44";

$search = "/[^search_for_me=](.*)[^\n]/";
echo preg_replace($search,$replace,$string)."\n\n";

echo 'done';

2 个解决方案

#1


1  

Try with this:

试试这个:

$string= <<<EOT
bla bla bla

search_for_me=12.21/13.31/14

bla bla bla
EOT;

printf("-String without replace:\n\n%s\n\n", $string);

$replace = '21.12/44.22/44';

$pattern = '/(?<=search_for_me\=)(.*)/';

$new_string = preg_replace($pattern, $replace, $string);

printf("-String with replace:\n\n%s", $new_string);

I use the positive lookbehind HERE

我在这里使用积极的外观

#2


1  

Here i am using regex to search and replace,

在这里我使用正则表达式来搜索和替换,

Regex: /(search_for_me).*?\n/, This will match search_for_me and till \n

正则表达式:/(search_for_me).*?\n/,这将匹配search_for_me并直到\ n

Replacement: '\1=21.12/44.22/44'."\n" Here \1 will contain first captured group search_for_me.

替换:'\ 1 = 21.12 / 44.22 / 44'。“\ n”这里\ 1将包含第一个捕获的组search_for_me。

Try this code snippet here

在此处尝试此代码段

<?php
/*
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string)."\n\n\n";
//April1,2003
*/
$string = file_get_contents('test.conf');

$string= <<<EOT
bla bla bla

search_for_me=12.21/13.31/14

bla bla bla
EOT;

echo $string."\n\n";

$replace = "21.12/44.22/44";

$search = "/(search_for_me)=.*?\n/";
echo preg_replace($search,'\1=21.12/44.22/44'."\n",$string)."\n\n";

#1


1  

Try with this:

试试这个:

$string= <<<EOT
bla bla bla

search_for_me=12.21/13.31/14

bla bla bla
EOT;

printf("-String without replace:\n\n%s\n\n", $string);

$replace = '21.12/44.22/44';

$pattern = '/(?<=search_for_me\=)(.*)/';

$new_string = preg_replace($pattern, $replace, $string);

printf("-String with replace:\n\n%s", $new_string);

I use the positive lookbehind HERE

我在这里使用积极的外观

#2


1  

Here i am using regex to search and replace,

在这里我使用正则表达式来搜索和替换,

Regex: /(search_for_me).*?\n/, This will match search_for_me and till \n

正则表达式:/(search_for_me).*?\n/,这将匹配search_for_me并直到\ n

Replacement: '\1=21.12/44.22/44'."\n" Here \1 will contain first captured group search_for_me.

替换:'\ 1 = 21.12 / 44.22 / 44'。“\ n”这里\ 1将包含第一个捕获的组search_for_me。

Try this code snippet here

在此处尝试此代码段

<?php
/*
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string)."\n\n\n";
//April1,2003
*/
$string = file_get_contents('test.conf');

$string= <<<EOT
bla bla bla

search_for_me=12.21/13.31/14

bla bla bla
EOT;

echo $string."\n\n";

$replace = "21.12/44.22/44";

$search = "/(search_for_me)=.*?\n/";
echo preg_replace($search,'\1=21.12/44.22/44'."\n",$string)."\n\n";