473,322 Members | 1,405 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,322 software developers and data experts.

split with regex

Hello All,

Been beating my head on the wall trying to get this working. Especially
because of my *tweener level of php and regex experience.

I have this $filename = C17AA001.000

I want to split it like this $a = C17AA $b = 001 $c = .000

I have tried this but can't get it to work.

list($a, $b, $c,) = split("^[0-9a-zA-Z]{5}[0-9]{3}\.[0-9]{3}",$filename);

echo $a;
echo $b;
echo $c;

What am I missing here? Two days of googling and php.net have not
cleared me up.

Patrick

*tweener - between novice and guru
May 4 '07 #1
3 1833
Patrick kirjoitti:
Hello All,

Been beating my head on the wall trying to get this working. Especially
because of my *tweener level of php and regex experience.

I have this $filename = C17AA001.000

I want to split it like this $a = C17AA $b = 001 $c = .000

I have tried this but can't get it to work.

list($a, $b, $c,) = split("^[0-9a-zA-Z]{5}[0-9]{3}\.[0-9]{3}",$filename);

echo $a;
echo $b;
echo $c;

What am I missing here? Two days of googling and php.net have not
cleared me up.
The reg ex argument you give to split is not the entire string pattern,
just the delimiter. You tell what pattern splits the string into pieces.
Now you're giving it the entire string pattern that does match the
whole string but so it basicly splits the string into two, the empty
before the string and the empty after the string.

You should be using preg_match 'stead of split. Try something like this:

preg_match("^[0-9a-zA-Z]{5}[0-9]{3}\.[0-9]{3}",$matches,$filename);
list($a, $b, $c,) = $matches;

HTH

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 4 '07 #2
You're using split wrong - it's for "splitting it on boundaries formed
by the case-sensitive regular expression" - the regex is the dividing
boundary, not the text you're matching.

Try this:

$filename = 'C17AA001.000';
preg_match("/([0-9a-zA-Z]{5})([0-9]{3})(\.[0-9]{3})/",$filename,
$matches);
//print_r($matches);
list($x, $a, $b, $c,) = $matches;//discard x
echo $a;
echo $b;
echo $c;

Aerik (a tweener myself)
http://www.wikidweb.com - the Wiki Directory of the Web
May 4 '07 #3
Aerik wrote:
You're using split wrong - it's for "splitting it on boundaries formed
by the case-sensitive regular expression" - the regex is the dividing
boundary, not the text you're matching.

Try this:

$filename = 'C17AA001.000';
preg_match("/([0-9a-zA-Z]{5})([0-9]{3})(\.[0-9]{3})/",$filename,
$matches);
//print_r($matches);
list($x, $a, $b, $c,) = $matches;//discard x
echo $a;
echo $b;
echo $c;

Aerik (a tweener myself)
http://www.wikidweb.com - the Wiki Directory of the Web

Sweet and thanks. That did it for me. And being able to get that part
working allowed me to finish and successfully test my php script which
basically renames a directory of files from

this

C17AC000.000
C17AC001.000
C17AC002.000
C17AC003.000
C17AC004.000
C17AC005.000
C17AC006.000

to this

C17AC.000
C17AC.001
C17AC.002
C17AC.003
C17AC.004
C17AC.005
C17AC.006

Regards,
Patrick
May 4 '07 #4

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

Similar topics

7
by: Chris | last post by:
hello, I have question about the re.I option for Regular Expressions: >>> import re >>> re.findall('x', '1x2X3', re.I) as expected finds both lower and uppercase x
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
5
by: lgbjr | last post by:
Hello All, I have the following type of string: "X:Y\Z.exe" "123" What I need is an array of strings with the information from within each set of quotes. I was trying to use a Regex.Split, but...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
7
by: Jordi Rico | last post by:
Hi, I know I can split a string into an array doing this: Dim s As String()=Regex.Split("One-Two-Three","-") So I would have: s(0)="One" s(1)="Two"
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
1
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
12
blazedaces
by: blazedaces | last post by:
Hello again. I'm trying to take as an input an ArrayList<String> and utilize String's .spit(delimiter) method to turn that into a String. I'm getting some kind of error though (I'll post the code...
1
by: mad.scientist.jr | last post by:
I am working in C# ASP.NET framework 1.1 and for some reason Regex.Split isn't working as expected. When trying to split a string, Split is returning an array with the entire string in element ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.