Jump to content

Archived

This topic is now archived and is closed to further replies.

the_butcher

matching a pattern on each line.

Recommended Posts

ok, have a text file, with contents in format:

"etc etc","etc etc"
"etc2","etc 22"
"12312","etc.. etc.."

Now, i had a regular expression which seems to work except for in PHP. that is;

/^"([^"]+)","([^"]+)"$/

The line start and end are required.

Share this post


Link to post
Share on other sites
The [tt]/m[/tt] modifier puts the pattern in multi-line mode, which allows ^ to match after a new line, and $ before. Modifiers are placed after the last delimiter, e.g.: [tt]/pattern/m[/tt].

A more extensible, and arguably cleaner solution follows:

[code]
<pre>
<?php

$tests = array(
'"etc etc","etc etc"',
'"etc2","etc 22"',
'"12312","etc.. etc.."'
);

foreach ($tests as $test) {
$array = preg_split('/[",]/', $test, -1, PREG_SPLIT_NO_EMPTY);
print_r($array);
}

?>
</pre>
[/code]

Share this post


Link to post
Share on other sites
i have tried /m, no luck. unfortunately i need to do this with just a regex, so your 2nd approach is neither valid.

Share this post


Link to post
Share on other sites
Try to modify your regex in this way, as said you have to use the "m" modifier, plus I've added an optional rule for windows couse It uses \r\n instead of \n.

[CODE]/^"([^"]+)","([^"]+)"\r?$/m[/CODE]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.