regular expression help w/ preg_split

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

regular expression help w/ preg_split

Post 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.
Last edited by discobean on Tue Jun 03, 2003 1:29 am, edited 1 time in total.
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

move this thread

Post by discobean »

can somebody move this thread into the correct group :?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

my pleasure ;)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

is "\n" mandatory or can it be something like \\n, too?
would be much easier to catch with pcre assertions
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post 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.
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post by discobean »

I just wrote a function to do what I needed..

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