Page 1 of 1

regular expression help w/ preg_split

Posted: Tue Jun 03, 2003 1:23 am
by discobean
I am hopeless writing regular expressions..

Hopefully somebody might help me out..

I'm looking to do a preg_split and split by newline characters (any new line characters), but not newline characters that are quoted,

ie. split this: example \n rah rah
but not this: example "\n" rah rah

and if possible split by any new line characters, ie. \n or \r or ^M etc...

I'm trying to write it now, but as I said earlier, I'm hopeless with regex.
I got a few more regex questions, but hopefully this'll point me in the right direction :P

Marz.

move this thread

Posted: Tue Jun 03, 2003 1:25 am
by discobean
can somebody move this thread into the correct group :?

Posted: Tue Jun 03, 2003 2:47 am
by volka
my pleasure ;)

Posted: Tue Jun 03, 2003 2:55 am
by volka
is "\n" mandatory or can it be something like \\n, too?
would be much easier to catch with pcre assertions

Posted: Tue Jun 03, 2003 4:16 am
by patrikG
Newlines in PHP-regEx are

\r\n

(yes, I know - it somehow looks a bit sore, but that's what it is). I am still on my first cup of coffee - my mind doesn't bend enough yet to think about quoted and non-quoted regEx.

Posted: Tue Jun 03, 2003 1:01 pm
by McGruff
My ambitions in life are to climb mount everest, swim the atlantic, take part in the Alaska grizzly wrestling championship without getting eaten, and learn regex.

Posted: Tue Jun 03, 2003 1:06 pm
by nielsene
you could probably do a three pass algorithm
first replace "\"\n\"" with NON_REPLACED_NEWLINE
then split on [\r\n]*
then reaplce NON_REPLACED_NEWLINE with "\n"

or something along those lines

Posted: Tue Jun 03, 2003 6:42 pm
by discobean
Hm... nielsene, I probably didn't give a good example, but the \n's won't actually be 100% always in between speech marks... so dunno, but its helping me look at it at a different perspective...

atm I'm just reading about the pcre assertions (volka style), I'll get there.

Posted: Tue Jun 03, 2003 8:19 pm
by discobean
I just wrote a function to do what I needed..

Looks like I'll NEVER learn regex properly :roll: