homepage Welcome to WebmasterWorld Guest from 174.129.130.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Regular expressions not working for me
Help with matching please
neiljones




msg:1275008
 11:14 pm on Nov 30, 2003 (gmt 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);
$numextracted=$matches[0];

Gets me nowhere with (a);
and
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.

 

coopster




msg:1275009
 1:59 pm on Dec 1, 2003 (gmt 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);
$numextracted=$matches[0];

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?
[webmasterworld.com...]
If so, are you trying to get the actual text string "buffer", which could be a variable string off the original string as well?

neiljones




msg:1275010
 4:22 pm on Dec 1, 2003 (gmt 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
and
12 this is the string

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

I have it all working as I want now.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved