Dare
Dare

Reputation: 1

preg replace specific character inside ()

How I can replace character * only inside () with character -?


For example:

$x="AUDI A4 Avant (8D5* B5)* A4 (8D2* B5)* A6 Avant (4A* C4)* A6 Avant (4B5* C5)* A6 (4A* C4)*A6 (4B2* C5)* AUDI (FAW) A6L Stufenheck (4B5* C5)* VW PASSAT Variant (3B5)* PASSAT (3B2)";

Output for $x would return

$x="AUDI A4 Avant (8D5- B5)*A4 (8D2- B5)*A6 Avant (4A- C4)*A6 Avant (4B5- C5)*A6 (4A- C4)*A6 (4B2- C5)*AUDI (FAW) A6L Stufenheck (4B5- C5)*VW PASSAT Variant (3B5)*PASSAT (3B2)";

Upvotes: 0

Views: 30

Answers (1)

Toto
Toto

Reputation: 91385

Have a try with:

Find what: (\([^*]*)\*([^*)]*\))
Replace with: $1-$2

Explanation:

(       : begining of group 1
  \(    : a parenthesis
  [^*]* : 0 or more character that is not an asterisk
)       : end of group 1
\*      : an asterisk
(       : begining of group 2
  [^*)]*: 0 or more character that is not an asterisk or a parenthesis
  \)    : a parenthesis
)       : end of group 2

Upvotes: 1

Related Questions