Converting regex from ruby to PHP -


i have been trying convert regular expression ruby php, have had little luck.

this ruby regular expression:

quoted_literal = /"[^"\\]*(?:\\.[^"\\]*)*"/ unquoted_literal = /[^\s=,][^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/ literal = /(#{quoted_literal}|#{unquoted_literal})/ pair = /#{literal}\s*=>\s*#{literal}/ 

and go in php:

 const pair = '/("[^"\\]*(?:\\.[^"\\]*)*"|[^\s=,][^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*)\s*=>\s*("[^"\\]*(?:\\.[^"\\]*)*"|[^\s=,][^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*)/'; 

however when run

$result = preg_match_all(self::pair, $input, $matches); 

i error:

preg_match_all(): compilation failed: unmatched parentheses @ offset 62


however when run through, http://www.phpliveregex.com/ test data:

"foo" => "bar", "foo" => bar, foo => "bar" 

it seems work fine.

not sure whats going on.

the problem lies backslashes.

i managed compile after removing backslashes.

then, replaced double slashes 4 of them, , preg_match_all() able compile regex too.

const pair = '/("[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^\s=,][^\s=,\\\\]*(?:\\\\.[^\s=,\\\\]*|=[^,>])*)\s*=>\s*("[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^\s=,][^\s=,\\\\]*(?:\\\\.[^\s=,\\\\]*|=[^,>])*)/'; 

you might have edit exact regex want. had compilation error because \\ fed regex engine \, escaped immediate square brackets. encode literal backslash, need use \\\\ - once string, , once regex engine.

string '\\\\'  --becomes--> regex \\ --becomes--> literal \ 

ruby doesn't have problem because regex syntax separate string syntax.

(related question: preg_match(): compilation failed: unmatched parentheses.)


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -