简体   繁体   中英

Is it a simplest substring in python?

Below is my code which get code(always in brackets, and always has 3 chars) from string :

raw_text='Spain (BCN)' #another examples: 'Italy (BGN)' , 'Germany (SXF)'
formatted_text=raw_text[raw_text.index('(')+1:len(raw_text)-1] # BCN,BGN,SFX

Can I write it simpler?

import re
raw_text='Spain (BCN)'
formatted_text = re.search(r"""
    (?<=\() # assert that the preceding character is a (
    \w{3}   # match three alphanumeric characters
    (?=\))  # assert that the following character is a )""", 
    raw_text, re.VERBOSE).group(0)

would be another way of doing it (with a regular expression).

If you are certain to have this format why not just use:

s.strip()[-4: -1]

Of course, it does not check the format of your string. If you want to do that, use the re module (regular expressions).

Hope this helps,

Dimi

Yip there sure is.

raw_text='Spain (BCN)  '
print raw_text.rstrip(" ")[-4:-1] 

Use rstrip to remove trailing spaces, eg trim. Then simply go back 4 chars, to -1 chars.

splicing a string is [start:stop] and you are stopping on len(raw_text)-1 - always the second-last character. If you know the code continues to the end of the string, and as you've said it is always three characters long, then:

 formatted_text=raw_text[-4:-1]

will extract the three characters that start 4 from the end of the string

>>> raw_text='Spain (BCN)'
>>> formatted_text=raw_text[raw_text.index('(')+1:len(raw_text)-1]
>>> formatted_text
'BCN'
>>> raw_text[raw_text.index('(')+1:-1]
'BCN'
>>>

What an allegedly non-brittle does:

>>> str = 'abcdefgh'
>>> str[str.find('(')+1:str.find(')')]
'abcdefg'
>>>

No, this is good enough. You could make a function that accepts a string and returns it formatted.

Also, don't go with len(raw_text)-1, because that will fail on bad data like "Italy (BGN) ".

def get_code(str):
     return str[str.find('(')+1:str.find(')')]

formatted_text = get_code(raw_text)

You can use a regex

>>> import re
>>> re.search('\((.{3})\)', 'Spain (BCN)').group(0)
'BCN'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM