php some regex without end
I am trying to learn regex and have a string, I want the beginning to be (not including)
.co开发者_C百科m/
and the end to be (not including)
">[tomato
I can make the basic regex ok, but it keeps including the tomato part in it, instead of ending just before it. So if I have
'sdf98uj3.com/sdjkh.sdf./sdf.sdf">[tomatoiosdf8uj3'
, then I want to return:
sdjkh.sdf./sdf.sdf
I am using preg_match in php. Any ideas?
preg_match('~\.com/(.*?)">\[tomato~', $str, $match);
$match[1]
contains your string. The (.*?)
is a capture group that captures every character between .com/
and ">[tomato
.
http://www.regular-expressions.info/ is a good start for learning regular expressions.
Could also be solved without regex, using strpos
(docs), strrpos
(docs) and substr
(docs):
$start = strpos($str, '.com/');
$end = strrpos($str, '">[tomato'); // or strpos to find the first occurance
$sub = substr($str, $start, $end-$start);
preg_match('/(.*?)\.com/(.*?)">\[tomato(.*)/', $text, $matches);
echo $matches[2];
精彩评论