Jump to content


Photo

matching a pattern on each line.


  • Please log in to reply
3 replies to this topic

#1 the_butcher

the_butcher
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 August 2006 - 12:25 AM

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.

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 23 August 2006 - 01:31 AM

The /m 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.: /pattern/m.

A more extensible, and arguably cleaner solution follows:

<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>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 the_butcher

the_butcher
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 August 2006 - 09:03 AM

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

#4 rea|and

rea|and
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 24 August 2006 - 07:44 AM

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.

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users