2017-07-27 15 views
-1

今日、私はこのコード行に出くわした:トラブル理解re.findallパターン構文

re.findall(r"#[^:]+:([^#]+)", str) 

私はfindall機能が探しているもののパターンについて非常に混乱しています。特にr"#[^:]+:([^#]+)"の意味は?

私は高校生ですので、簡単に言えばそれはすごいでしょう!

+2

[documentation](https://docs.python.org/2/library/re.html)を読んで質問を更新してください。 –

+0

ドキュメントの補足として、正規表現をテストし、[regex101.com](https://regex101.com)でどのように動作するかについて説明します。 –

+0

どうすれば更新できますか? – Joe

答えて

3

それはこの意味です:

 
# => matches the character # literally (case sensitive) 

[^:] => Match a single character that is not : 

+ => Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) and is applied to the [^:] 

: => matches the character : literally (case sensitive) 

([^#]+) => Capturing Group 

    [^#] => Match a single character not present in this list (match anything other than #) 

    + => Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) and is applied to [^#] 

をリテラルrが引用された文字列がその中に何かがコンパイラに特別な意味を持っていない、あなたはいけないことを意味し、rawテキストであることを意味することに注意してください任意の文字を二重引用符でもエスケープする必要があります!

+0

これはこの正規表現の[regex101の説明](https://regex101.com)のわずかな編集のようです。これを示すためにOPに役立つだろう。 –

+0

ありがとうございました – Joe

+0

'str =" k-ポイント数:816バンド数:52イオン数:8 "関数は何を返しますか?それは '' k点数:816 ''または単に' '816''でしょうか? – Joe