python string pattern matching
new_str="@@2@@*##1"
new_str1="@@3@@*##5##7"
How to split the above string in python
for val in new_str.split("@@*"):
logging.debug("=======")
logging.debug(val[2:]) // will give
for st in val.split("@@*"):
//how to get the values after ## in new_str and new_st开发者_StackOverflowr1
I don't understand the question.
Are you trying to split a string by a delimiter? Then use split
:
>>> a = "@@2@@*##1"
>>> b = "@@3@@*##5##7"
>>>
>>> a.split("@@*")
['@@2', '##1']
>>> b.split("@@*")
['@@3', '##5##7']
Are you trying to strip extraneous characters from a string? Then use strip
:
>>> c = b.split("@@*")[1]
>>> c
'##5##7'
>>> c.strip("#")
'5##7'
Are you trying to remove all the hashes (#
) from a string? Then use replace
:
>>> c.replace("#","")
'57'
Are you trying to find all the characters after "##"
? Then use rsplit
with its optional argument to split only once:
>>> a.rsplit("##",1)
['@@2@@*', '1']
精彩评论