Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-04-2010, 07:55 PM   PM User | #1
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
Regex Number followed by symbol issue

Why is the 3rd term not -6_?

PHP Code:
$lefteq="-x+2+10x-9-6_";
$lct preg_match_all('/(\-)?(\d+)(\-|\+|\=|\>|\<|\_)/'$lefteq$matchPREG_PATTERN_ORDER);
for (
$x=0;$x<$lct;$x++){
$curvalue=$match[0][$x];
echo 
"curvalue = " $curvalue "<br />";
$curvalue=substr($curvalue,0,strlen($curvalue)-1);
array_push($lconst,$curvalue);

which produces

PHP Code:
curvalue 2+
curvalue = -9-
curvalue 6_ 
mathceleb is offline   Reply With Quote
Old 11-04-2010, 08:02 PM   PM User | #2
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Code:
$lct = preg_match_all('/(\-)?(\d+?)(\-|\+|\=|\>|\<|\_)/', $lefteq, $match, PREG_PATTERN_ORDER);
or:

Code:
$lct = preg_match_all('/(\-)?(\d+)(\-|\+|\=|\>|\<|\_)/u', $lefteq, $match, PREG_PATTERN_ORDER);
or:

Code:
$lct = preg_match_all('/^(\-)?(\d+)(\-|\+|\=|\>|\<|\_)/', $lefteq, $match, PREG_PATTERN_ORDER);
should do as you expect.
MattF is offline   Reply With Quote
Old 11-04-2010, 08:28 PM   PM User | #3
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
2 of them did the same as what I had, and the one with the line starter ^ showed nothing.
mathceleb is offline   Reply With Quote
Old 11-04-2010, 09:29 PM   PM User | #4
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
What is it supposed to match? All or part of the string? If partial, which part? Btw, do a search on Google or such for 'PCRE cheat sheet'. It'll help with the specifics of regex.
MattF is offline   Reply With Quote
Old 11-04-2010, 09:55 PM   PM User | #5
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
It is supposed to match any number, positive or negative, followed by an operator sign or an underscore.

The only trouble I have in unit testing is that a negative number followed by a negative number comes up as negative positive. Everything else works like a charm.

I'll check the cheatsheet out.
mathceleb is offline   Reply With Quote
Old 11-04-2010, 10:04 PM   PM User | #6
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
Tried this instead, I removed the underscore from the end of $lefteq:
I'm looking for any number not followed by a letter. This gives the last negative number, but also gives the 1 in the number 10.

PHP Code:
$lct preg_match_all('/(\-?)(\d+)(?![a-z])/'$lefteq$matchPREG_PATTERN_ORDER); 
mathceleb is offline   Reply With Quote
Old 11-04-2010, 10:10 PM   PM User | #7
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
Fixed. I needed boundaries on the number. This solves my problem:

PHP Code:
$lct preg_match_all('/(\-?)\b(\d+)\b(?![a-z])/'$lefteq$matchPREG_PATTERN_ORDER); 
mathceleb is offline   Reply With Quote
Old 11-04-2010, 10:19 PM   PM User | #8
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Quote:
Originally Posted by mathceleb View Post
I'm looking for any number not followed by a letter.
I'm a bit rusty, but try this:

Code:
preg_match_all('/(\d+)(?![a-z])/', $lefteq, $match, PREG_PATTERN_ORDER);
Try looking in $match[1] instead of $match[0] too.


Edit: You shouldn't need to use the boundary marker.
MattF is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:28 PM.


Advertisement
Log in to turn off these ads.