Hello all,
I've been dealing for something for an hour with no success and it driving me crazy.
my perl is too weak .
here is my problem:
I have a string wich may or may not contain numbers.
from that string I need to get a substring as followed:
from the second digit take 4 steps back.
example 1:
string-hr-0223-bla
result:
hr-02
example 2:
yd-99string
result:
yd-99
I know it's not so hard but it's driving me NUTS!
need some help with regular expression
Moderator: General Moderators
Code: Select all
<?php
$pattern = '!(.{3}\d{2})!';
$subjects = array(
'string-hr-0223-bla',
'yd-99string'
);
foreach($subjects as $sub)
{
preg_match($pattern, $sub, $match);
print_r($match);
}
?>that doesn't matter
Code: Select all
<?php
$pattern = '!(.{3}\d{2})!';
$subjects = array(
'string-hr-0223-bla',
'yd-99string',
'adfsdfsd-978sdfsadfsdfas-9087'
);
foreach($subjects as $sub)
{
preg_match($pattern, $sub, $match);
print_r($match);
}
?>unless you use ^ resp. $ it doesn't matter where in subject the pattern matches, first match->done.Array
(
[0] => hr-02
[1] => hr-02
)
Array
(
[0] => yd-99
[1] => yd-99
)
Array
(
[0] => sd-97
[1] => sd-97
)