By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

regex for preg_split

P: n/a
Tom
A puzzle for you regular expression wizards out there.

Looking for a regex that will split up a string like the following on
any pipe (|) not inside brackets:

a b | a { b |{c | cd}} d | a b c

Correct result would be:

array ( [0] ='a b', [1] ='a { b |{c | cd}} d', [2] ='a b c')

I've found a couple that get close, but nothing that quite does what
I'd like.

(Or is this better handled without regex?)

Thanks,
Tom

Jan 25 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Wed, 24 Jan 2007 20:19:51 -0800, Tom wrote:
A puzzle for you regular expression wizards out there.

Looking for a regex that will split up a string like the following on
any pipe (|) not inside brackets:

a b | a { b |{c | cd}} d | a b c

Correct result would be:

array ( [0] ='a b', [1] ='a { b |{c | cd}} d', [2] ='a b c')

I've found a couple that get close, but nothing that quite does what
I'd like.

(Or is this better handled without regex?)

Thanks,
Tom
The following appear to work, at least with your example and a couple of
alternatives:

/(?:\s\|\s)(?![\w\s\{]*\})/

Matches a space a pipe and a space NOT followed by zero or more word,
space, or left brace characters and one right brace.

Results in:

Array
(
[0] =a b
[1] =a { b |{c | cd}} d
[2] =a b c
)

YMMV of course. I imagine it's likely to break on more complex strings.

--
Schluppy
Jan 25 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.