I'm totally new to regular expressions and I'm trying to get something like this:
["Group", "s1", "s2", "Group2"]
from a string:
string = "_GRP_Group||s1||s2||Group2||"
All I have now is:
word = re.findall(r'([^\|]+)', string)
which just leaves out the pipe and I get this:
['_GRP_Group', 's1', 's2', 'Group2']
Is there a way to get rid of the _GRP_
prefix?
Based on your comments on other answers, it sounds like the _GRP_
prefix is a prefix to the string rather than to each individual split value?
Try this:
string = "_GRP_Group||s1||s2||Group2||"
word = re.findall(r"(?:_GRP_)?([^|]+)", string)
You don't need to use regular expressions to split the first string by ||
or remove the prefix _GRP_
. You can just use split
and slicing:
words = "_GRP_Group||s1||s2||Group2||"[5:].split('||')
The slice [5:]
will exclude the first five characters from the string.
If you didn't know where _GRP_
would occur, you could use replace
:
words = "_GRP_Group||s1||s2||Group2||".split('||')
words = [word.replace("_GRP_", "") for word in words]
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.