BartmanEH
BartmanEH

Reputation: 232

RegEx for PHP preg_replace swapping matches and matching multiple instances

I'm looking for a RegEx for preg_replace in PHP for the following scenario:

I'm a noob at PHP and RegEx and have been playing around in the cool test arena with things like preg_replace("/^\"(?<=- )/", ""$2 $1$3"", $input_lines); with horrible results. Thanks for help!

Upvotes: 0

Views: 267

Answers (1)

vks
vks

Reputation: 67968

([^," -]*)\s*-\s*([^," ]*)

Try this.See demo.

http://regex101.com/r/hI0qP0/20

$re = "/([^\", -]*)\\s*-\\s*([^,\" ]*)/m"; 
$str = "\"Bjerre- Jonas, Jorgensen- Silas, Wohlert- Johan, Madsen- Bo\""; 
$subst = "$2 $1"; 

$result = preg_replace($re, $subst, $str);

Upvotes: 1

Related Questions