-2

I have a string which looks like this avg(foo) from where i want to extract only foo. For this I am using

import re
re.sub(")( avg","","avg(foo)")

But this is not working. Where am I going wrong?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Rajarshi Bhadra
  • 1,826
  • 6
  • 25
  • 41
  • Please be more clear as to what you want. Do you want occurrences of `'foo'`, or any occurrence of any string inside the parentheses of `'avg(…)'`, or inside any parentheses of a function, or inside any parentheses at all, or something else? – Rory Daulton Jun 11 '18 at 14:39

3 Answers3

2

First I'm off you might want to use r-strings for you pattern, and in addition you need to change your pattern a bit r"avg\((.*)\)" should suffice.

import re

re.findall(r"avg\((.*)\)", "avg(foo)")
# Will return ['foo']

So what's happening here?
We're using re.findall to find all occurrences matching the pattern.

The pattern can be breaken down to:

  1. avg\( something that starts with avg(
  2. (.*) subgroup of any string (this could be modified to match a stricter pattern)
  3. finally \) which will match a single ")".
Hultner
  • 3,710
  • 5
  • 33
  • 43
0

We may use re.search here, with a capture group to extract the text inside the function call to avg():

f_search = re.search(r'\((.*?)\)', 'avg(foo)', re.IGNORECASE)

if f_search:
    print f_search.group(1)

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

First of all, parenthesis represent the capture groups in regular expression syntax, thus if you want to match the literal parenthesis you need to escape them. Secondly, if you want to find a particular string you should use re.search or re.match() which in this case since the sub-string is not appeared at the leading of the string you should use re.search(). Thirdly, a proper regex for matching everything between parenthesis depends on whether you want to match nested ones or not. One may use r"\(([^)(]*)\) to match everything except parenthesis characters. Another option would be \((.*)\) to match everything between parenthesis but still it depends on your expected format.

Demos:

>>> m = re.search(r"\(([^)(]*)\)","avg(foo)")
>>> m.group(1)
'foo'
>>> m = re.search(r"\((.*)\)","avg(f(o)o)")
>>> m.group(1)
'f(o)o'
>>> m = re.search(r"\(([^)(]*)\)","avg(f(o)o)")
>>> m.group(1)
'o'
Mazdak
  • 105,000
  • 18
  • 159
  • 188