0

I want to handle 2 cases:

  1. (string) -> string

  2. @string otherStuff -> string

how can it be done?

2
  • 4
    "What have you tried?" Did it work? If not, why not? Commented Apr 23, 2012 at 1:10
  • why was this downgraded? Commented Apr 23, 2012 at 1:26

3 Answers 3

2
>>> re.search(r'\(([^)]+)\)|@([^ ]+) ', '(string)').groups()
('string', None)
>>> re.search(r'\(([^)]+)\)|@([^ ]+) ', '@string otherStuff').groups()
(None, 'string')
Sign up to request clarification or add additional context in comments.

Comments

1
import re

def getstring(string):
    testforstring = re.search("\((.*)\)",string)
    if testforstring:
        return testforstring.group(1)
    testforstring = re.search("@(.*?)\s+.*",string)
    if testforstring:
        return testforstring.group(1)
    return None

Allows you to do:

>>> print getstring('(hello)')
hello
>>> print getstring('@hello sdfdsf')
hello

Comments

0

Will this work for you?

In [45]: s='(string)' 
In [46]: s = s[1:-1]

In [47]: s
Out[47]: 'string'

and

In [48]: s = '@string otherstuff'
In [49]: s=' '.join(s.split()[1:])  

In [50]: s
Out[51]: 'otherstuff'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.