【问题标题】:How to match the pattern using re using the commas? [closed]如何使用逗号匹配模式? [关闭]
【发布时间】:2016-11-19 11:26:41
【问题描述】:

如何使用 re 匹配以下模式?

2016-02-13 02:00:00.0,3525,http://www.heatherllindsey.com/2016/02/my-husband-left-his-9-5-job-for-good-it.html,158,0,2584490

我使用python的split()函数将属性分离出来,但由于数据量很大,进程因内存错误而被杀死。

【问题讨论】:

  • 在正则表达式方面您应该非常具体。您没有提供任何尝试,我们无法确定您真正需要什么。 只需使用.+ 来匹配此字符串 是对此类模糊问题的有效答案。请澄清并添加您到目前为止编写的代码和正则表达式。

标签: python regex python-2.7


【解决方案1】:

如果你放长版本的字符串会更好。 那么你怎么能做到呢?这就是答案:

import re
str = "2016-02-13 02:00:00.0,3525,http://www.heatherllindsey.com/2016/02/my-husband-left-his-9-5-job-for-good-it.html,158,0,2584490"
pattern = re.compile("(.*?),", re.DOTALL) #we use re.DOTALL to continue splitting after endlines.
result = pattern.findall(str) #we can't find the last statement (2584490) because of the pattern so we will apply second process
pattern = re.compile("(.*?)", re.DOTALL)
str2 = str[-50:-1]+str[-1] #we take last partition of string to find out last statement by using split() method

result.append(str2.split(",")[-1])
print result

它有效...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多