Andrei Surdu
Andrei Surdu

Reputation: 2341

Regex to find and replace

I have this text:

_e('Don\'t change this 1...', 'text_to_change');
_e('Don\'t change this extra...', 'text_to_change');
_e('Don\'t change this te...', 'text_to_change');
_e('Don\'t change this text...', 'text_to_change');

How can I replace 'text_to_change' to something else in PHP. I think I should use preg_replace but the true is that I have no idea how to use regex.

The result I need:

_e('Don\'t change this 1...', 'some_text');
_e('Don\'t change this extra...', 'some_text');
_e('Don\'t change this te...', 'some_text');
_e('Don\'t change this text...', 'some_text');

I need it to do with regex no other solutions. Thank you.

Upvotes: 0

Views: 62

Answers (3)

Avinash Raj
Avinash Raj

Reputation: 174836

Below regex would match the text inside the second single quotes. Just replace the matched string with some_text.

(?<= \')[^']*

DEMO

Your PHP code would be,

<?php
$string = <<<'EOT'
_e('Don\'t change this 1...', 'text_to_change');
_e('Don\'t change this extra...', 'text_to_change');
_e('Don\'t change this te...', 'text_to_change');
_e('Don\'t change this text...', 'text_to_change');
EOT;
$pattern = "~(?<= \')[^']*~";
$replacement = 'some_text';
echo preg_replace($pattern, $replacement, $string);
?>

Output:

_e('Don\'t change this 1...', 'some_text');
_e('Don\'t change this extra...', 'some_text');
_e('Don\'t change this te...', 'some_text');
_e('Don\'t change this text...', 'some_text');

Explanation:

  • (?<= \') Lookbehind is actually used to look after a string which matches a particular pattern. In our case, it looks after the string '(ie, space followed by a single quote). Second quotes satisfy this condition. So the regex engine sets the matching marker only after to the second single quote.

  • [^']* Matches any character not of single quote, zero or more times.

Upvotes: 1

hwnd
hwnd

Reputation: 70742

You can use the following regular expression:

_e\('.*',\s*'\K[^']*

Explanation

Example:

$text = <<<DATA
_e('Don\'t change this 1...', 'text_to_change');
_e('Don\'t change this extra...', 'text_to_change');
_e('Don\'t change this te...', 'text_to_change');
_e('Don\'t change this text...', 'text_to_change');
DATA;

$text = preg_replace("/_e\('.*',\s*'\K[^']*/", 'some_text', $text);
echo $text;

Output:

_e('Don\'t change this 1...', 'some_text');
_e('Don\'t change this extra...', 'some_text');
_e('Don\'t change this te...', 'some_text');
_e('Don\'t change this text...', 'some_text');

Upvotes: 2

simpleigh
simpleigh

Reputation: 2914

You can use preg_replace, but there's not much point if you're just trying to change out a fixed string. Why not just use str_replace:

$newString = str_replace('text_to_change', 'some_text', $oldString);

It would be worth using preg_replace if you're worried that the text 'text_to_change' might appear somewhere else. In this case I guess you could write a regex to match those specific lines. This might look something like:

$pattern = "^_e\('(.+)', 'text_to_change'\);$";
$newString = preg_replace($pattern, "_e('\1', 'some_text');", $oldString);

If you're using Linux it might be easier to use find and sed from the command-line rather than whip up a custom PHP script:

find . -type f -name '*.php' -exec sed -ir "s/_e\('(.+)', 'text_to_change'\);/_e('\1', 'some_text');/" {} \;

Here we use find to locate all files (-type f) in the current directory (.) with a name like *.php. We then call sed to change in-place (-i) the files given.

Upvotes: 1

Related Questions