Preg_Split / Regular Expressions

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
cfytable
Forum Commoner
Posts: 29
Joined: Thu May 12, 2005 3:36 pm

Preg_Split / Regular Expressions

Post by cfytable »

I need help with two string-splitting cases.

1) The first set of strings take the following form:
ABC111111, ZZZ2222222, XXXXX34536474

In this case, I would like the split to occur after the last occurence of an alphabetic letter. For instance, using the first two examples above:

Array[0] = ABC
Array [1] = 111111

Array[0] = XXXXX
Array [1] = 34536474

What regular expression would I use with preg_split in this case?

2) In the second case, there are some letters before and after a common number sequence. For instance:
EB1234666666, CBB123477777777

In this case, I would like to split the string after the '1234' sequence. For instance, using the first two examples above:

Array[0] = EB1234
Array [1] = 666666

Array[0] = CBB1234
Array [1] = 77777777

What regular expression would I use with preg_split in this case?

Any help would be appreciated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what have you tried so far?
cfytable
Forum Commoner
Posts: 29
Joined: Thu May 12, 2005 3:36 pm

Post by cfytable »

Well, I tried the following for #2, but it's obviously not correct. It splits the string, but eliminates the '1234,' rather than store in the first array position. As I said, I'm not good with regular expressions.

$portions = preg_split('/1234/',$ID);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Here's a tip: you don't actually want preg_split() here but preg_match().
Post Reply