RegEx for PHP preg_replace swapping matches and matching multiple instances -
i'm looking regex preg_replace in php following scenario:
- example string
"bjerre- jonas, jorgensen- silas, wohlert- johan, madsen- bo"
- desired string
"jonas bjerre, silas jorgensen, johan wohlert, bo madsen"
- string csv field , double quotes enclosures , part of string
- any number of occurrences may exist including none - example has 4
- there consistent
-
match on separating matches swapped
i'm noob @ php , regex , have been playing around in cool test arena things preg_replace("/^\"(?<=- )/", ""$2 $1$3"", $input_lines);
horrible results. help!
([^," -]*)\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);
Comments
Post a Comment