2

There is a variable which is:

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

I am trying to find a part which includes "s()".

I tried using:

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

But it seems that It is not able to search for characters that includes ( )

Is there a way to find it and print it in python?

kgb26
  • 183
  • 2
  • 14
  • 1
    I'd recommend that you familiarise yourself with the regular expression syntax: https://docs.python.org/2/library/re.html#regular-expression-syntax (for example, `*` doesn't do what you probably think it does). – NPE Oct 30 '16 at 14:43
  • Why is he getting downvoted? – pylang Oct 31 '16 at 04:35

1 Answers1

3

Your regex is the problem here.

You can use:

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

RegEx Breakup:

s     # match letter s
\(    # match literal (
[^)]* # Using a negated character class, match 0 more of any char that is not )
\)    $ match literal (
  • r is used for raw string in Python.
anubhava
  • 761,203
  • 64
  • 569
  • 643