[英]Split string by comma and space or space
我有两个示例字符串,我想用“、”(如果存在)或“”来分割它们。
x = ">Keratyna 5, egzon 2, Homo sapiens"
y = ">101m_A mol:protein length:154 MYOGLOBIN"
拆分应该只执行一次以恢复两条信息:
id, description = re.split(pattern, string, maxsplit=1)
For ">Keratyna 5, egzon 2, Homo sapiens" -> [">Keratyna 5", "egzon 2, Homo sapiens"]
对于">101m_A mol:protein length:154 MYOGLOBIN" -> [">101m_A", "mol:protein length:154 MYOGLOBIN"]
我想出了以下模式: ",\\s+|\\s+", ",\\s+|^,\\s+", "[,]\\s+|[^,]\\s+"
,但是这些都不起作用。
我提出的解决方案是使用异常:
try:
id, description = re.split(",\s+", description, maxsplit=1)
except ValueError:
id, description = re.split("\s+", description, maxsplit=1)
但老实说,我讨厌这种解决方法。 我还没有找到任何合适的正则表达式模式。 我该怎么做?
您可以使用
^((?=.*,)[^,]+|\S+)[\s,]+(.*)
请参阅正则表达式演示。 详情:
^
- 字符串的开头((?=.*,)[^,]+|\S+)
- 第 1 组:如果有 a ,
则在除换行符之外的任何零个或多个字符之后尽可能多地匹配除换行符之外的一个或多个字符,
, 或匹配一个或多个非空白字符[\s,]+
- 零个或多个逗号/空格(.*)
- 第 2 组:除换行符之外的零个或多个字符尽可能多请参阅Python 演示:
import re
pattern = re.compile( r'^((?=.*,)[^,]+|\S+)[\s,]+(.*)' )
texts = [">Keratyna 5, egzon 2, Homo sapiens", ">101m_A mol:protein length:154 MYOGLOBIN"]
for text in texts:
m = pattern.search(text)
if m:
id, description = m.groups()
print(f"ID: '{id}', DESCRIPTION: '{description}'")
Output:
ID: '>Keratyna 5', DESCRIPTION: 'egzon 2, Homo sapiens'
ID: '>101m_A', DESCRIPTION: 'mol:protein length:154 MYOGLOBIN'
[不满足问题]你只需要检查字符串中是否有逗号
def split(n):
if ',' in n:
return n.split(', ')
return n.split(' ')
您可以在第一次出现时拆分,
或者在没有出现的空格上拆分,
使用交替向右:
, | (?!.*?, )
模式匹配:
,
匹配,
|
或者(?.?*,, )
负前瞻,断言右边不是,
例子
import re
strings = [
">Keratyna 5, egzon 2, Homo sapiens",
">101m_A mol:protein length:154 MYOGLOBIN"
]
for s in strings:
print(re.split(r", | (?!.*?, )", s, maxsplit=1))
Output
['>Keratyna 5', 'egzon 2, Homo sapiens']
['>101m_A', 'mol:protein length:154 MYOGLOBIN']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.