confusion about regular expressions in php

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Muhammad Zeeshan
Forum Newbie
Posts: 1
Joined: Sun Jan 24, 2016 3:40 am

confusion about regular expressions in php

Post by Muhammad Zeeshan »

Can anybody explain me about the following code.
The following code
<?php
$a = 'first line
second line
';
$b = '/l...$/';
$match = preg_match_all($b,$a,$matches);

print_r($matches);

?>
is showing the output

Array ( [0] => Array ( ) )

instead of
Array ( [0] => Array ( [0] => line ) )

But the following code

<?php
$a = 'first line
second line
';
$b = '/l....$/';
$match = preg_match_all($b,$a,$matches);

print_r($matches);

?>
is showing
Array ( [0] => Array ( [0] => line ) )
instead of

Array ( [0] => Array ( ) )
Can anyone explain me why it is working in reverse order in my computer?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: confusion about regular expressions in php

Post by requinix »

Muhammad Zeeshan wrote:The following code
<?php
$a = 'first line
second line
';
$b = '/l...$/';
$match = preg_match_all($b,$a,$matches);

print_r($matches);

?>
is showing the output

Array ( [0] => Array ( ) )

instead of
Array ( [0] => Array ( [0] => line ) )
$ only matches the end of a line if you pass the /m flag.

Code: Select all

/l...$/m
Muhammad Zeeshan wrote:But the following code

<?php
$a = 'first line
second line
';
$b = '/l....$/';
$match = preg_match_all($b,$a,$matches);

print_r($matches);

?>
is showing
Array ( [0] => Array ( [0] => line ) )
instead of

Array ( [0] => Array ( ) )
It isn't doing that... You sure you got that right?
b03tz
Forum Newbie
Posts: 3
Joined: Wed May 11, 2016 3:14 pm

Re: confusion about regular expressions in php

Post by b03tz »

The one with 3 dots is matching line, the with 4 isn't. At least not on regex101.com

/l...$/m matches "line" (on the first line)
/l...$/ matches "line" (on the second line)

And:
/l....$/m and /l....$/ matches nothing, so indeed, are you sure you got that right?

[edit] wow old thread nvm [/edit]
Post Reply