[英]Regex capture between certain characters
I'm quite new to Python and regex.我对 Python 和正则表达式很陌生。 I'm almost there but fail to fix this issue after 6 hours.
我快到了,但 6 小时后未能解决此问题。 Hopefully someone can help.
希望有人能提供帮助。
My string is as follows:我的字符串如下:
str_1 = & peers & & apples & & lemon juice & & Strawberries & & Mellon &
I would like a new list that contains: ['peers','apples','lemon juice','Strawberries','Mellon']
.我想要一个包含以下内容的新列表:
['peers','apples','lemon juice','Strawberries','Mellon']
。 So without all the whitespace and the &
signs.所以没有所有的空格和
&
标志。
My code is as follows:我的代码如下:
list_1 = re.compile(r'(?<=&)(.*?)(?=&)').findall(str_1)
However, I get something like this:但是,我得到这样的东西:
list_1 = [' peers ', ' ', ' apples ', ' ', ' lemon juice ', ' ', ' Strawberries ', ' ', ' Mellon']
Can someone please help to get:有人可以帮忙得到:
['peers','apples','lemon juice','Strawberries','Mellon']
You don't need regexes for this你不需要正则表达式
>>> str_1 = '& peers & & apples & & lemon juice & & Strawberries & & Mellon &'
>>> ls = [x.strip() for x in str_1.split('&')]
>>> ls = [x for x in ls if x]
>>> ls
['peers', 'apples', 'lemon juice', 'Strawberries', 'Mellon']
If you still want a regex, then如果你仍然想要一个正则表达式,那么
>>> re.findall(r'[^& ][^&]*[^& ]', str_1)
['peers', 'apples', 'lemon juice', 'Strawberries', 'Mellon']
If you have to use a regex , you can use如果你必须使用正则表达式,你可以使用
re.findall(r'[^&\s]+(?:[^&]*[^&\s])?', str_1)
See the regex demo .请参阅正则表达式演示。 Details :
详情:
[^&\s]+
- one or more chars other than &
and whitespace - (?:[^&]*[^&\s])?
[^&\s]+
- 除了&
和空格之外的一个或多个字符 - (?:[^&]*[^&\s])?
- an optional sequence of any chars other than &
and then a char other than a &
or whitespace. &
之外的任何字符的可选序列,然后是除&
或空格之外的字符。 See the Python demo :请参阅Python 演示:
import re
str_1 = "& peers & & apples & & lemon juice & & Strawberries & & Mellon & "
print( re.findall(r'[^&\s]+(?:[^&]*[^&\s])?', str_1) )
# => ['peers', 'apples', 'lemon juice', 'Strawberries', 'Mellon']
A non-regex solution can look like一个非正则表达式的解决方案看起来像
[x.strip() for x in str_1.split('&') if x.strip()]
See this Python demo .请参阅此 Python 演示。 Here, you split a string with
&
chars and only keep the items that are not empty or are all whitespace with leading/trailing spaces stripped.在这里,您用
&
字符拆分一个字符串,只保留不为空或全是空白的项目,前导/尾随空格被去除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.