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
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.