简体   繁体   English

如何从这两个字符串中提取字符串信息?

[英]How to extract string information from these two strings?

I want to write a single regular expression code to extract the string from these two strings:我想编写一个正则表达式代码来从这两个字符串中提取字符串:

string1 = '@HISEQ:625:HC2T5BCXY:1:1101:1177:2101'
string2 = '@SRR7216015.1 HISEQ:630:HC2VKBCXY:1:1101:1177:2073/1'

I want to extract the string right after the @ until it hit the end or a space to get我想在@ 之后立即提取字符串,直到它到达末尾或空格为止

HISEQ:625:HC2T5BCXY:1:1101:1177:2101 from string1

or或者

SRR7216015.1 from string2

So, how to do it.那么,该怎么做。 I've tested a bunch of the regular expression code but couldn't do it.我已经测试了一堆正则表达式代码,但无法做到。

Below is the code I tried:下面是我试过的代码:

string1 = '@HISEQ:625:HC2T5BCXY:1:1101:1177:2101'
string2 = '@SRR7216015.1 HISEQ:630:HC2VKBCXY:1:1101:1177:2073/1'
pattern1 = re.compile(r'@(\w*.*:*\d*:*\w*:*\d*:*\d*[$|\s])')
print(pattern1.search(string1).group(1))

Thanks in advance!提前致谢!

Just use只需使用

@(\S+)

and take the first group.并采取第一组。 Lookarounds or alternations - as suggested in other answers - are expensive.环顾四周或交替 - 正如其他答案中所建议的 - 是昂贵的。

You could use this regex for that:你可以使用这个正则表达式:

(?<=@).*?(?= |$)

Use lookarounds.使用环视。 (?<=@) checks for an @ signt before, (?= |$) matches an spaces or end of string. (?<=@)检查 @ 符号, (?= |$)匹配空格或字符串结尾。 .* mathes everything between .*数学计算之间的一切

https://regex101.com/r/p7kI2O/1 https://regex101.com/r/p7kI2O/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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