Enjoy an ad free experience by logging in. Not a member yet?
Register .
11-04-2010, 07:55 PM
PM User |
#1
Regular Coder
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
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 , $match , PREG_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_
11-04-2010, 08:02 PM
PM User |
#2
Senior Coder
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
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.
11-04-2010, 08:28 PM
PM User |
#3
Regular Coder
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
2 of them did the same as what I had, and the one with the line starter ^ showed nothing.
11-04-2010, 09:29 PM
PM User |
#4
Senior Coder
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
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.
11-04-2010, 09:55 PM
PM User |
#5
Regular Coder
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
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.
11-04-2010, 10:04 PM
PM User |
#6
Regular Coder
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
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 , $match , PREG_PATTERN_ORDER );
11-04-2010, 10:10 PM
PM User |
#7
Regular Coder
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
Fixed. I needed boundaries on the number. This solves my problem:
PHP Code:
$lct = preg_match_all ( '/(\-?)\b(\d+)\b(?![a-z])/' , $lefteq , $match , PREG_PATTERN_ORDER );
11-04-2010, 10:19 PM
PM User |
#8
Senior Coder
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
Quote:
Originally Posted by
mathceleb
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.
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 06:28 PM .
Advertisement
Log in to turn off these ads.