Free Devi
Free Devi

Reputation: 65

How to replace text without changing quoted string with regex

I want to replace

$this->input->post("product_name");

with

$post_data["product_name"];

I want to use notepad++ regex, but I couldn't find proper solution

In find --> $this->input->post("[\*w\]"); In replace --> $post_data["$1"];

but its not working

Upvotes: 1

Views: 615

Answers (3)

Guillaume D
Guillaume D

Reputation: 2326

  • To replace

$this->input->post("product_name");

by

$post_data["product_name"];

do replace, with regex activated

this->input->post\("(.*)"\);

by

post_data\["\1"\];

  • The \x with x a number, corresponds to the x-th match catched with the parenthesis. Here we catch any character inside this->input->post(XXXX);

  • Don't forget to escape special character with \. Your special characters were []()

Upvotes: 0

Michał Turczyn
Michał Turczyn

Reputation: 37367

Use this pattern to match desired text \$this->input->post\(("[^"]+")\);

And replace it with pattern \$post_data\[\1\]

Explanation:

\$this->input->post - matach $this->input->post literally

\(("[^"]+")\); - match (literally, then match double quates and everything between them with "[^"]+" and store inside first capturing group, then match ); literally

enter image description here

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

The $this->input->post("[\*w\]"); pattern does not work because:

  • $ is a special char matching the end of a line, you need to use \$ to match it as a literal char
  • [\*w'\] is a malformed pattern as there is no matching unescaped ] for the [ that opens a character class. Also, w just matches w, not any letter, digit or underscore, \w does that.

You may use

Find What: \$this->input->post\("(\w*)"\);
Replace With: $post_data["$1"];

If there can be any char inside double quotes use .*? instead of \w*:

Find What: \$this->input->post\("(.*?)"\);

Regulex graph:

enter image description here

NPP test:

enter image description here

Upvotes: 1

Related Questions