我尝试将这个php-函数转换为Python:
function trouveunebrique($contenu, $debut, $fin) {
$debutpos = strpos($contenu, $debut);
$finpos = strpos($contenu, $fin, $debutpos);
if ($finpos == 0) {
$finpos = strlen($contenu);
}
$nbdebut = strlen($debut);
if ($debutpos > 0) {
$trouveunebrique = substr($contenu, ($debutpos + $nbdebut), ($finpos - $debutpos - $nbdebut));
}
else {
$trouveunebrique = "";
}
return (trim($trouveunebrique));
}
我搜索了here,但找不到解决方案。 我也试过了:
^{pr2}$
要在Python中获取子字符串(以及与之相关的任何子序列),请使用slice notation,这与索引类似,但括号之间至少包含一个冒号:
您已经找到了strpos()的等价物:string对象上的str.find()方法。另请注意,您可以像在PHP函数中那样为其提供附加索引:
^{pr2}$如果找不到子字符串,则返回-1。否则,它的行为与PHP等效。在
如果我理解正确,你想在}结束?在
contenu
中找到一个子串,从debut
开始,到{所以如果你
你想要:
^{pr2}$如果是这样的话,您需要的是
(string).find
,它的行为类似于您的strpos
所以你的方法是这样的:
或者简而言之:
另外,由于您希望您的
fin
位于您的debut
之后,以下操作应该有效:相关问题 更多 >
编程相关推荐