1

次の変数があります。

line="s(a)='asd'"

「s()」を含む部分を探しています。

私は使用してみました:

re.match("s(*)",line)

ただし、 ( )を含む文字は検索できないようです。

それを見つけてPythonで印刷する方法はありますか?

4

1 に答える 1

3

あなたの正規表現はここで問題です。

以下を使用できます。

>>> line="s(a)='asd'"
>>> print re.findall(r's\([^)]*\)', line)
['s(a)']

正規表現の分割:

s     # match letter s
\(    # match literal (
[^)]* # Using a negated character class, match 0 more of any char that is not )
\)    $ match literal (
  • rPython では生の文字列に使用されます。
于 2016-10-30T14:43:32.953 に答える