Page 1 of 1
regex question
Posted: Fri Jul 27, 2012 11:09 pm
by global_erp_solution
I notice all regex in PHP begins with / character. Does this signal the beginning of line?
Code: Select all
preg_match('/abc 12(de)??/','xxxyyy abc 12de, gghh',$result);
var_export($result);
will yield array (0=>'abc 12',)
while
Code: Select all
preg_match('/abc 12(de)?/','xxxyyy abc 12de, gghh',$result);
var_export($result);
will yield array (0=>'abc 12', 1=>'de',)
why is this? the difference is just the additional question mark in the regex.
Re: regex question
Posted: Sat Jul 28, 2012 2:01 am
by TildeHash
global_erp_solution wrote:I notice all regex in PHP begins with / character. Does this signal the beginning of line?
No. It signifies the beginning of the regular expression. "^" signifies the beginning of a line and "$" signifies the end.
global_erp_solution wrote:
Code: Select all
preg_match('/abc 12(de)??/','xxxyyy abc 12de, gghh',$result);
var_export($result);
will yield array (0=>'abc 12',)
while
Code: Select all
preg_match('/abc 12(de)?/','xxxyyy abc 12de, gghh',$result);
var_export($result);
will yield array (0=>'abc 12', 1=>'th',)
why is this? the difference is just the additional question mark in the regex.
A question mark normally means "non-greedy", two question marks should never be needed. However, I don't see what you're trying to do with this regular expression. What output
do you want?
By the way:
Code: Select all
preg_match('/abc 12(de)?/','xxxyyy abc 12de, gghh', $result);
var_export($result);
Gives me:
[text]array ( 0 => 'abc 12de', 1 => 'de', )[/text]
And the question mark isn't required at all, neither is the "(de)" in your first example.
Re: regex question
Posted: Sat Jul 28, 2012 11:50 pm
by global_erp_solution
yes, it should be de. sorry for the mistake. editted the post
Re: regex question
Posted: Sun Jul 29, 2012 4:47 pm
by Live24x7
this is the shortest and the sweetest php regex tutorial:
http://knowpapa.com/php-regex/