Regex Python 2.7正则表达式从drupal插件获取版本?

Regex Python 2.7正则表达式从drupal插件获取版本?,regex,python-2.7,Regex,Python 2.7,如何获取2.x-dev、6.x-2.x、6.x-2.3、7.x-1.x-dev、6.x-16.x-1.6。 谢谢我希望您对此解决方案感到满意: import re data = """ Views 2.x-dev Viewfield 6.x-2.x, xxxx-xx-xx Version 6.x-2.3 ctools 7.x-1.x-dev XML sitemap 6.x-1, 2009-08-24 6.x-1.6 """ print data 重新导入 data=”“” 视图2.x-dev

如何获取
2.x-dev、6.x-2.x、6.x-2.3、7.x-1.x-dev、6.x-16.x-1.6

谢谢

我希望您对此解决方案感到满意:

import re
data = """
Views 2.x-dev
Viewfield 6.x-2.x, xxxx-xx-xx
Version 6.x-2.3
ctools 7.x-1.x-dev
XML sitemap 6.x-1, 2009-08-24
6.x-1.6
"""
print data
重新导入
data=”“”
视图2.x-dev
视野6.x-2.x,xxxx-xx-xx
版本6.x-2.3
ctools 7.x-1.x-dev
XML站点地图6.x-11909-08-24
6.x-1.6
"""
##解决方案1:
##1)查找与模式匹配的所有结果
##“.x-”以一种不贪婪的方式
##(这就是为什么“?”)
##2)处理找到的匹配项并删除任何“,”
结果=[]
对于re.findall(r“[0-9]\.x{1}-.*x?”,数据)中的结果:
results.append(result.split(“,”[0]”)
打印(结果)
##解决方案2:(也是我最喜欢的)
##用与模式匹配的单词创建一个列表
##一开始是.x
##这些单词是将“数据”与
##使用
##常规括号的非捕获版本。
结果=[x代表x在重新分割中(r'(?:,|\s)\s*',数据)\
如果重新匹配(r'^[0-9].x',x)]
打印(结果)

我希望您对该解决方案感到满意:

import re
data = """
Views 2.x-dev
Viewfield 6.x-2.x, xxxx-xx-xx
Version 6.x-2.3
ctools 7.x-1.x-dev
XML sitemap 6.x-1, 2009-08-24
6.x-1.6
"""
print data
重新导入
data=”“”
视图2.x-dev
视野6.x-2.x,xxxx-xx-xx
版本6.x-2.3
ctools 7.x-1.x-dev
XML站点地图6.x-11909-08-24
6.x-1.6
"""
##解决方案1:
##1)查找与模式匹配的所有结果
##“.x-”以一种不贪婪的方式
##(这就是为什么“?”)
##2)处理找到的匹配项并删除任何“,”
结果=[]
对于re.findall(r“[0-9]\.x{1}-.*x?”,数据)中的结果:
results.append(result.split(“,”[0]”)
打印(结果)
##解决方案2:(也是我最喜欢的)
##用与模式匹配的单词创建一个列表
##一开始是.x
##这些单词是将“数据”与
##使用
##常规括号的非捕获版本。
结果=[x代表x在重新分割中(r'(?:,|\s)\s*',数据)\
如果重新匹配(r'^[0-9].x',x)]
打印(结果)

如评论中所述:

import re

data = """
Views 2.x-dev
Viewfield 6.x-2.x, xxxx-xx-xx
Version 6.x-2.3
ctools 7.x-1.x-dev
XML sitemap 6.x-1, 2009-08-24
6.x-1.6
"""
## Solution 1: 
## 1) Finds all results that matches with the pattern 
## '<number>.x-<other_chars>' in a not greedy way 
## (that is why the '?')
## 2) Treat the found match and remove any ','

results = []
for result in re.findall( r"[0-9]\.x{1}\-.*x?", data ):
    results.append(result.split(",")[0])

print (results)

## Solution 2: (and my favourite)
## Create a list with those words that matches the pattern 
## <number>.x at their beginning
## The words are the result of splitting the 'data' with the
## split function from the regex module with a
## non-capturing version of regular parentheses.

result = [ x  for x in re.split(r'(?:,|\s)\s*', data) \ 
           if re.match(r'^[0-9].x',x) ]

print (result)


参见。

如评论中所述:

import re

data = """
Views 2.x-dev
Viewfield 6.x-2.x, xxxx-xx-xx
Version 6.x-2.3
ctools 7.x-1.x-dev
XML sitemap 6.x-1, 2009-08-24
6.x-1.6
"""
## Solution 1: 
## 1) Finds all results that matches with the pattern 
## '<number>.x-<other_chars>' in a not greedy way 
## (that is why the '?')
## 2) Treat the found match and remove any ','

results = []
for result in re.findall( r"[0-9]\.x{1}\-.*x?", data ):
    results.append(result.split(",")[0])

print (results)

## Solution 2: (and my favourite)
## Create a list with those words that matches the pattern 
## <number>.x at their beginning
## The words are the result of splitting the 'data' with the
## split function from the regex module with a
## non-capturing version of regular parentheses.

result = [ x  for x in re.split(r'(?:,|\s)\s*', data) \ 
           if re.match(r'^[0-9].x',x) ]

print (result)


请参阅。

分享您的研究成果对每个人都有帮助。告诉我们您尝试了什么,以及为什么它不能满足您的需求。这表明你花了时间来帮助自己,它使我们避免重复显而易见的答案,最重要的是,它帮助你得到一个更具体和相关的答案!另请参见Try as regex.regex='\d\.-.xdev\w]+'谢谢分享您的研究成果对每个人都有帮助。告诉我们您尝试了什么,以及为什么它不能满足您的需求。这表明你花了时间来帮助自己,它使我们避免重复显而易见的答案,最重要的是,它帮助你得到一个更具体和相关的答案!另请参见Try as regex.regex='\d\.-.xdev\w]+'谢谢