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.