473,573 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular expression, (preg_split etc...), some help please.

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 [0]=1, [1]=2,[2]=3.

Now if i have

'1,"2,3"' i could split it using preg_split("/(?<!\"),/\d", '1,"2,3"') and i
correctly get [0]=1, [1]="2,3".

But it clearly does not work in some more advanced cases, for example

'1," 2 , 3"' or '1,"2 , 3 "' mainly because the /d is no longer useful.

So how can i search for a regular expression that is *not within*
apostrophes?

I think i might have to write my own split function especially if i have an
extreme case like, '1," 2 , \" 3"', (note the escape apostrophe).

Many thanks for you input.

Sims

Jul 17 '05 #1
16 3536
In article <c0************ *@ID-162430.news.uni-berlin.de>,
"Sims" <si*********@ho tmail.com> wrote:
Consider the string

'1,2,3', I can split it using, preg_split("/,/", '1,2,3') and i correctly
get [0]=1, [1]=2,[2]=3.

Now if i have

'1,"2,3"' i could split it using preg_split("/(?<!\"),/\d", '1,"2,3"') and i
correctly get [0]=1, [1]="2,3".

But it clearly does not work in some more advanced cases, for example

'1," 2 , 3"' or '1,"2 , 3 "' mainly because the /d is no longer useful.


Is the overall goal to extract numbers having arbitrary separators? If so,
how about splitting on "/\D+/"?

--
CC
Jul 17 '05 #2
In article <c0************ *@ID-162430.news.uni-berlin.de>,
"Sims" <si*********@ho tmail.com> wrote:
Consider the string

'1,2,3', I can split it using, preg_split("/,/", '1,2,3') and i correctly
get [0]=1, [1]=2,[2]=3.

Now if i have

'1,"2,3"' i could split it using preg_split("/(?<!\"),/\d", '1,"2,3"') and i
correctly get [0]=1, [1]="2,3".

But it clearly does not work in some more advanced cases, for example

'1," 2 , 3"' or '1,"2 , 3 "' mainly because the /d is no longer useful.


(Oops, never mind that last post. )

--
CC
Jul 17 '05 #3
On Thu, 12 Feb 2004 08:49:59 +0200, Sims wrote:
I need some help to split data using regular expression


No you don't :) http://www.php.net/fgetcsv
Jul 17 '05 #4

"Ewoud Dronkert" <me@privacy.net > wrote in message
news:n3******** *************** *********@4ax.c om...
On Thu, 12 Feb 2004 08:49:59 +0200, Sims wrote:
I need some help to split data using regular expression


No you don't :) http://www.php.net/fgetcsv


Sorry but that is reading from a file, i am reading from a line.
I do want the same sort of output but not from a file.

I will have a look at the C code to see if can create a php function to
achieve what i need.

Sims.
Jul 17 '05 #5
"Sims" <si*********@ho tmail.com> wrote in message news:<c0******* ******@ID-162430.news.uni-berlin.de>...
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 [0]=1, [1]=2,[2]=3.

Now if i have

'1,"2,3"' i could split it using preg_split("/(?<!\"),/\d", '1,"2,3"') and i
correctly get [0]=1, [1]="2,3".

But it clearly does not work in some more advanced cases, for example

'1," 2 , 3"' or '1,"2 , 3 "' mainly because the /d is no longer useful.

So how can i search for a regular expression that is *not within*
apostrophes?

I think i might have to write my own split function especially if i have an
extreme case like, '1," 2 , \" 3"', (note the escape apostrophe).

Many thanks for you input.

Sims


My guess is following RegExp.

[SNIP]

$str = '1," 2 ,\" 3 ", 4, 5';
$arr = preg_match_all( '#(?<=")\s*[\d]+.*(?=[^\\\]")|[\d]+#U',$str,$matc hes);
print_r($matche s);

[/SNIP]

Try and let me know if it does not work as per your requirements.

--
Cheers,
Rahul Anand
Jul 17 '05 #6

My guess is following RegExp.

[SNIP]

$str = '1," 2 ,\" 3 ", 4, 5';
$arr = preg_match_all( '#(?<=")\s*[\d]+.*(?=[^\\\]")|[\d]+#U',$str,$matc hes); print_r($matche s);

[/SNIP]


If it is a guess then it is brilliant, thanks : ).

It works 99.9%.

The only time it does not work is when you have something like... $str =
'1," 2 ,\" 3a", 4, 5';, (Note the 'a' after the '3').
Your RegExp removes the last item, (letter),
Your expression is very very complicated for me so i am going to spend some
time trying to work it all out.

Thanks a million.
Jul 17 '05 #7
On Thu, 12 Feb 2004 11:42:08 +0200, Sims wrote:
No you don't :) http://www.php.net/fgetcsv


Sorry but that is reading from a file, i am reading from a line.


So post the data to a script, open a filepointer to "php://input" and
read from that. Or write data to diskfile and read from diskfile (duh).
Jul 17 '05 #8

"Ewoud Dronkert" <me@privacy.net > wrote in message
news:ak******** *************** *********@4ax.c om...
On Thu, 12 Feb 2004 11:42:08 +0200, Sims wrote:
No you don't :) http://www.php.net/fgetcsv
Sorry but that is reading from a file, i am reading from a line.


So post the data to a script, open a filepointer to "php://input" and
read from that. Or write data to diskfile and read from diskfile


Sorry that is simply not good practice at all, in fact it is very bad
programming.

Reading/Writing data to file? Just to use a function, i would bring my
server to a stand still.
I think i will rather use a more realistic function like the one offered by
Rahul.
(duh).


Thanks.

Sims
Jul 17 '05 #9
On Thu, 12 Feb 2004 21:59:30 +0200, Sims wrote:
Sorry that is simply not good practice at all, in fact it is very bad
programming.


Then provide some more details of the environment/circumstances/typical
situation. You gave none. For all we know, you were trying to convert a
small one-time output by your heartrate monitor or something.

Please don't put me down for failing prerequisites you did not mention.
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1888
by: tdl | last post by:
Hi everybody, I'm a regular expressions newbie; I read many tutorials, but need more experience before becoming indipendent in this field. I need a little help from you. How can I split such a formatted string: UPPERCASEID0: texttexttexttext UPPERCASEID1: textagaintextagain textagaintextagain UPPERCASEID2: textagaintextagain
8
1350
by: brendan | last post by:
my regular expression knowledge is admittedly amateurish ... I have spent an hour trying to get this to work without any great success, but am in the middle of a big project so I can't waste a day fiddling around to get it to work (although I might have to if someone here can't help). I thought I would throw it out to the group to see if...
9
470
by: www.douglassdavis.com | last post by:
I am using the preg_match function (in PHP) that uses perl regular expressions. Apparently I don't really understand regular expressions though. Could some one explain this? If this is the regular expression /^\s*(\d+\.\d+)|(\.\d+)|(\d+)\s*$/ How does this:
2
1464
by: hillcountry74 | last post by:
Hi, I'm stuck with this regular expression from past 2 days. Desperately need help. I need a regular expression that will allow all characters except these *:~<>' This is my code in VB.Net- Dim regex As System.Text.RegularExpressions.Regex
4
5128
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following testMyColumn{1, 100}Test Basically I want the regular expression to add the word test infront of the
2
3007
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text index on one column, and punctuation in the column was causing some problems down the line. This column is used only for full text indexing, and...
3
1338
by: Lucky | last post by:
hi guys, i'm practising regular expression. i've got one string and i want it to split in groups. i was trying to make one regular expression but i didn't successed. please help me guys. i'm using .NET 2.0's Regular expression class. here is the string.
3
2554
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
3
1822
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular expression that only returns part of a string. For Example I have a multi-line text fragment like below:
0
7784
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8204
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7795
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6424
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1309
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.