473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

regex for preg_split

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
1 1773
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter | last post by:
L.S. I am trying to cut a string into substrings. The string at hand looks something like this: $data='one|two|three|four\nfive|six|seven|eight'; //observe the chr(10) in the middle I...
3
by: Sebastian Araya | last post by:
Hello, I have a string like this: var1: value1...valueI var2: value1...valueJ ... varN: value1...valueK this is an example: breakfast: coffee eggs lunch: sandwich apple dinner: chicken...
5
by: Chamomile | last post by:
I have to split strings of the type: $str1 =' Large ladies hats 1.365 0.334'; $str2 = 'Pins 0.335 0.22'; into separate variables (or array members) : say,...
16
by: Sims | last post by:
Hi, I need some help to split data using regular expression Consider the string '1,2,3', I can split it using, preg_split("/,/", '1,2,3') and i correctly get =1, =2,=3. Now if i have
15
by: voipcanada | last post by:
is there any thing we can write to keep the right txt to the = sign in the preg_split. from the following lines DST-NUMBER-IN=0033512877596, DST-NUMBER-OUT=98751#33512877596,...
2
by: www.douglassdavis.com | last post by:
lets say I have a line of text with multiple spaces between each word.. I am not sure how many spaces. I would like to take the line of text, and change it to put only one space between the...
3
by: none | last post by:
Hello: Using PHP 4.3.11 The line: $strDate = preg_split('//', $my_date); works to break apart a date in the form mm/dd/yyyy or mm.dd.yyyy or mm-dd-yyyy but the line: $strDate =...
0
by: tiggman | last post by:
Why preg_split works only if the char # before the delimiter is 21 or less? example php code: ============= $right = "{Hello Im very aaaaaa {cool|not cool}}"; print $right."<br>"; $pattern =...
9
by: conspireagainst | last post by:
I'm having quite a time with this particular problem: I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.