简体   繁体   English

相当于substr和strpos的php在Python中

[英]Php equivalent of substr and strpos in Python

I try to transform this php -function into Python : 我尝试将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));
}

I searched here but i could't find the solution. 在这里搜索但找不到解决方案。 I also tried this: 我也试过这个:

   def trouveunebrique(contenu, debut, fin)
        debutpos = haystack.find(contenu, debut)
        finpos = haystack.find(contenu, fin)
        if (finpos == 0)
            finpos = len(contenu)
        nbdebut = len(debut)
        if (debutpos > 0):
            trouveunebrique = substr(contenu, (debutpos + nbdebut), (finpos - debutpos - nbdebut))
        else:
            trouveunebrique = ""
        return trouveunebrique.strip()

To get substrings in Python (and any subsequences for that matter) use slice notation , which is similar to indexing but contains at least one colon between brackets: 要在Python中获取子字符串(以及该问题的任何子序列),请使用切片符号 ,它类似于索引编制,但在方括号之间至少包含一个冒号:

>>> "Hello world"[4:7]
'o w'
>>> "Hello world"[:3]
'Hel'
>>> "Hello world"[8:]
'rld'

You figured out strpos() equivalent already: str.find() method on string objects. 您已经找出了strpos()等效项:字符串对象上的str.find()方法。 Also note that you can provide an additional index to it like in your PHP function: 还要注意,您可以像在PHP函数中那样为其提供附加索引:

debutpos = contentu.find(debut)
# ...
finpos = contenu.find(fin, debutpos)

It returns -1 when substring is not found. 当找不到子字符串时,它将返回-1。 Otherwise, it behaves like PHP equivalent. 否则,其行为类似于PHP。

So if I understood correctly, you want to find a substring in contenu starting by debut and finish by fin ? 因此,如果我理解正确,那么您想在contenudebut开始找到一个子字符串,并以fin contenu

So if you set up 因此,如果您设置

>>> str   = "abcdefghi"
>>> debut = "bcd"
>>> fin   = "hi"

You want to do : 您想做:

>>> trouveunebrique(str, debut, fin)
bcdefghi

If that's the case, what you are looking for is (string).find which behave like your strpos 如果是这种情况,您正在寻找的是(string).find ,其行为类似于您的strpos

So your method will looks like this : 因此,您的方法将如下所示:

def trouveunebrique(contenu, debut, fin):
  indice_debut = contenu.find(debut)
  indice_fin = contenu.find(fin)
  return contenu[indice_debut : indice_fin + len(fin)]

Or in short : 或简而言之:

def trouveunebrique(contenu, debut, fin):
 return contenu[contenu.find(debut):contenu.find(fin) + len(fin)]

Also since you want your fin to be after your debut , the following should work : 另外,由于您希望findebut ,因此以下步骤应该有效:

def trouveunebrique(contenu, debut, fin):
  indice_debut = contenu.find(debut) # find the first occurence of "debut"
  indice_fin = contenu[indice_debut:].find(fin) # find the first occurence of "fin" after "debut"
  return contenu[indice_debut : indice_debut + indice_fin + len(fin)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM