0

Is there a way, using a regular expression (or few regular expressions in cascade), to convert a csv like text into something similar to a php array declaration, having an arbitrary number of fields?

For instance, the regular expression should go from this:

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

to this:

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

Is that possible using just regular expressions? Do you have suggestions about a practical implementation?

2 Answers 2

1

You could do multiple search and replaces that search for ^ *(\w+)(.*)\r\n *(\w+) and replace with !\1=\3\r\n\2\r\n, assuming Windows style line endings. Then run another simple edit to remove the leading ! characters. Using the example from the 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 two more replacements it completes leaving:

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

The remaining edits would need to tidy up ! and the . characters.

The search string allows optional spaces at the beginning of the line the ^ *. It then looks for a field-name (\w+) and the rest of that line (.*) and a line separator \r\n. Next it looks for optional spaces then a field-value with *(\w+).

The replacement just reorders the found fields and adds a leading ! so that a subsequent replace-all operation will not find thefields already matched.

Not exactly sure how to map this on to PHP, but it works fine in Notepad++ version 6.5.5.

Sign up to request clarification or add additional context in comments.

5 Comments

I'll try this! Just be careful: the dots imply more fields, not just dots
The idea is good, but the regexp just doesn't work... have you tried it under a pcre environment?
@clami219 have you used the "correct" line endings for your PCRE version? (Note that PCRE has many meanings.)
PCRE stands for Perl Compatible Regular Expressions. Anyway, you are right, the problem was with the line endings! Good job!
@clami219 I meant that there are several systems that claim to provide PCREs but they all provide slightly different facilities. I am happy that my answer helped you.
1

You can't do that with pure regex. But you can do it in PHP with arrays simply:

$text = "field1 field2 field3 field4 ...
          value1 value2 value3 value4 ...";

$arr = explode("\n", $text);

$field = explode(' ', $arr[0]);
$value = explode(' ', $arr[1]);

Then you can use a foreach loop to do this

1 Comment

That's not how you use functions in PHP. $arr = explode("\n", $text);, but anyway this wouldn't help the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.