Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to substring a string?

I have a string "MenuItem {Open source}".

How can I get the string Open source from my string?

e.g.

str1 = "MenuItem {Open source}"

perform some actions to set string two to be...

print str2  # 'Open source'

How can I acheive this using python or jython?

like image 561
Colin Avatar asked Dec 10 '22 12:12

Colin


1 Answers

You can get it with a regular expression.

>>> import re
>>> str1 = "MenuItem {Open source}"
>>> re.search(r'{(.*)}', str1).group(1)
'Open source'

You can also get it by splitting the string on the { and } delimiters (here I use str.rsplit rather than str.split to make sure it splits at the right-most match):

>>> str1.rsplit('{')[1].rsplit('}')[0]
'Open source'
like image 62
Chris Morgan Avatar answered Dec 21 '22 00:12

Chris Morgan