regex - Regular Expression from csv like to php array declaration -



regex - Regular Expression from csv like to php array declaration -

is there way, using regular look (or few regular expressions in cascade), convert csv text similar php array declaration, having arbitrary number of fields?

for instance, regular look should go this:

field1 field2 field3 field4 ... value1 value2 value3 value4 ...

to this:

"field1" => "value1", "field2" => "value2", "field3" => "value3", "field4" => "value4", ...

is possible using regular expressions? have suggestions practical implementation?

you multiple search , replaces search ^ *(\w+)(.*)\r\n *(\w+) , replace !\1=\3\r\n\2\r\n, assuming windows style line endings. run simple edit remove leading ! characters. using illustration question

field1 field2 field3 field4 ... value1 value2 value3 value4 ...

the first replacement yields:

!field1=value1 field2 field3 field4 ... value2 value3 value4 ...

the next replacement yields:

!field1=value1 !field2=value2 field3 field4 ... value3 value4 ...

after 2 more replacements completes leaving:

!field1=value1 !field2=value2 !field3=value3 !field4=value4 ... ...

the remaining edits need tidy ! , . characters.

the search string allows optional spaces @ origin of line ^ *. looks field-name (\w+) , rest of line (.*) , line separator \r\n. next looks optional spaces field-value *(\w+).

the replacement reorders found fields , adds leading ! subsequent replace-all operation not find thefields matched.

not sure how map on php, works fine in notepad++ version 6.5.5.

regex preg-replace pcre

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -