python
  • regex
  • string
  • 2012-01-02 20 views 23 likes 
    23

    私はすでにフォーラムからいくつかの情報を抽出しています。私は好きではない事は、サブ文字列"<font color="black"><font face="Times New Roman">""<font color="green"><font face="Arial">"あるPythonを使用してサブストリングを削除

    string = 'i think mabe 124 + <font color="black"><font face="Times New Roman">but I don\'t have a big experience it just how I see it in my eyes <font color="green"><font face="Arial">fun stuff' 
    

    :それは私が今持っている生の文字列です。私はこれ以外の文字列の残りの部分を保ちたいと思います。結果は次のようになります

    resultString = "i think mabe 124 + but I don't have a big experience it just how I see it in my eyes fun stuff" 
    

    どうすればいいですか?実際に私はフォーラムから上記の文字列を抽出するために美しいスープを使用しました。今、私は部分を削除するために正規表現を好むかもしれません。

    +0

    この文字列は、現在動作していない、それがサポートしてくれてありがとう@ThiefMaster両方 '" 'と' ' ' – juliomalegria

    +0

    内部を持っています。どのように私はそれを削除するだろうか?それはjulio.alegria @ –

    +0

    確かに残念ですしてくださいだけであなたがいくつかのテストをしたい場合は、文字列として始まりと終わりの間のものを扱います。ありがとうございます –

    答えて

    53
    import re 
    re.sub('<.*?>', '', string) 
    "i think mabe 124 + but I don't have a big experience it just how I see it in my eyes fun stuff" 
    

    re.sub機能は、通常のexpresionを取り、2番目のパラメータで文字列内のすべての一致を置き換えます。この場合、すべてのタグ('<.*?>')を検索し、何も指定しないで置き換えます('')。

    ?は、貪欲でない検索ではreで使用されます。

    re moduleについての詳細

    +8

    あなたは私のヒーローです –

    +0

    @ Wenhao.SHE私は助けてくれてありがとう – juliomalegria

    +0

    これは非常に役に立ちます..ありがとう私は私のプロジェクトのためにtwitterのつぶやきの言及を削除するためにこれを使用しました - ? '、' '、tweetText) – sumanth232

    10
    >>> import re 
    >>> st = " i think mabe 124 + <font color=\"black\"><font face=\"Times New Roman\">but I don't have a big experience it just how I see it in my eyes <font color=\"green\"><font face=\"Arial\">fun stuff" 
    >>> re.sub("<.*?>","",st) 
    " i think mabe 124 + but I don't have a big experience it just how I see it in my eyes fun stuff" 
    >>> 
    
    +7

    あなたはまたすばらしいです –

    関連する問題