Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regular expressions not working for me

Help with matching please

11:14 pm on Nov 30, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 14, 2003
votes: 0

The situation is as follows. I have some lines of data in the following format.
"123buffersome more text that may have numbers in it."
Where 123 could be a number from 1-999.

How do I
(a) get the number 123 off as a separate variable.
(b) how do I separate the some more text ..... bit without the buffer.

I am making no progress with this at all.

preg_match("/[0-9]/", $variable);

Gets me nowhere with (a);
for (b)
$separatedbit = preg_replace ("/^[0-9]+/","",$variable);
Gets the numbers off at the begining when there are 2 numbers but does nothing when there is a single digit.

I seem to be managing fine with the rest of PHP it is just the wretched reg exps that are giving me the problem.
Everything always seems to work with Perl for me.
Perhaps it is because I feel like I may be coming down with a bout of flu.

1:59 pm on Dec 1, 2003 (gmt 0)


WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
votes: 0

Assuming $variable in your example refers to the text string:

$variable = "123buffersome more text that may have numbers in it.";

You forgot to provide the matches parameter on your preg_match. I also added the + quantifier to grab 1 or more digits:

// preg_match("/[0-9]/", $variable); // <-- forgot matches parameter!
preg_match("/[0-9]+/", $variable, $matches);

And the preg_replace works fine as is, whether the text string contains 1 digit or more. Unless I am misunderstanding something here? Is this relative to the following post, neiljones?
If so, are you trying to get the actual text string "buffer", which could be a variable string off the original string as well?
4:22 pm on Dec 1, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 14, 2003
votes: 0

You're right the preg_replace does work. It was a problem with the data! hidden characters! Duh! No wonder I could not get it to work. Thanks. Your help is much appreciated.
I got hidden characters in some of my data when writing another piece of code. There won't be any in the finished version.

This is the trouble when you are new at something. It is more easy to assume that you are not doing something that is rather complicated in the correct manner than to assume there is a simpler problem elsewhere.

It is not related to the previous posting except that I learned the method I am using from there and thus there is a similarity.

The word buffer is there to be discarded. Essentially what I am doing is sorting some randomly sorted but numbered data. The easiest way is to tag the numbers on the beginning and do a natsort() on the array.

The word buffer is there to distinguish between
1 2this is the string
12 this is the string

which become
1buffer2this is the string
12bufferthis is the string.

I have it all working as I want now.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members