python - 获取python中第一次出现的字符串
问题描述
假设我的字符串是:s='[{abc:, xyz}, {something}, {and so on}]'
我想首先出现 and 之间的所有内容{
,}
即在这种情况下它将是{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}
推荐阅读
- internet-explorer - 让 vba 等到资源管理器准备好
- mysql - 用于计算表的两列(组合)中的数据的 SQL 查询
- bash - 如何在 bash 中使用并行命令和 find 命令
- java - 发生 HttpsURLConnection 400 Bad Request 错误
- neo4j - 如何在 Neo4j Cypher 的 CASE WHEN THEN 子句中执行密码查询
- react-native - 在“react-native-navigation”中将数据从侧面菜单传递到主屏幕
- javascript - 在 Angular 7 中压缩生产 main.js
- python - 未定义模型对象时,django rest 序列化字符串
- ssis - SSIS 错误:无法解密受保护的 XML 节点
- angular - ngModel 在与 formControlName 相同的表单字段上。支持使用反应形式的 ngModel 输入属性和 ngModelChange 事件已弃用