获取字符串在 python 中的第一次出现
Get the first occurrence of a string in python
假设我的字符串是:s='[{abc:, xyz}, {something}, {and so on}]'
我想获得 {
和 }
之间的所有内容的第一次出现,即在这种情况下它将是 {abc:, xyz}
这是我试过的
import re
s='[{abc:, xyz}, {something}, {and so on}]'
re.findall('(\{.*\})', s)[0]
import re
pattern = r'\{\w+\s*:,*\s*\w+\}'
s='[{abc:, xyz}, {abcd: 890}, {a :908}]'
res = re.findall(pattern, s)
print(res[0]) # output {abc:, xyz}
re.findall('(\{[a-z0-9, :]+\})', s)[0]
假设我的字符串是:s='[{abc:, xyz}, {something}, {and so on}]'
我想获得 {
和 }
之间的所有内容的第一次出现,即在这种情况下它将是 {abc:, xyz}
这是我试过的
import re
s='[{abc:, xyz}, {something}, {and so on}]'
re.findall('(\{.*\})', s)[0]
import re
pattern = r'\{\w+\s*:,*\s*\w+\}'
s='[{abc:, xyz}, {abcd: 890}, {a :908}]'
res = re.findall(pattern, s)
print(res[0]) # output {abc:, xyz}
re.findall('(\{[a-z0-9, :]+\})', s)[0]