Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 新行前任意字数的正则表达式_Python_Regex - Fatal编程技术网

Python 新行前任意字数的正则表达式

Python 新行前任意字数的正则表达式,python,regex,Python,Regex,我分析了段落中的一些文本,我想将其拆分以插入表中 字符串如下所示: [“一些文本不确定有多少个数字或是否有任何特殊字符等,但我不在乎,我只希望此字符串中的所有文本\n 123多一些文本(50%和更多文本)\n”] 我想做的是在新行之前分割出第一个文本字符串,不管它是什么。我开始尝试这个[A-Za-z]*\s*[A-Za-z]*\s*,但很快就意识到,这个字符串中的文本是可变的,因此无法剪切它 然后我想取第二个字符串中的数字,如下所示: \d+ 最后,我想得到第二个字符串中的百分比,以下内容似乎

我分析了段落中的一些文本,我想将其拆分以插入表中

字符串如下所示:

[“一些文本不确定有多少个数字或是否有任何特殊字符等,但我不在乎,我只希望此字符串中的所有文本\n 123多一些文本(50%和更多文本)\n”]

我想做的是在新行之前分割出第一个文本字符串,不管它是什么。我开始尝试这个
[A-Za-z]*\s*[A-Za-z]*\s*
,但很快就意识到,这个字符串中的文本是可变的,因此无法剪切它

然后我想取第二个字符串中的数字,如下所示:

\d+
最后,我想得到第二个字符串中的百分比,以下内容似乎适用:

\d+(%)+
我计划在函数中使用这些函数,但在编译第一部分的正则表达式时遇到了困难?我还想知道我为第二部分准备的正则表达式是否是最有效的

更新:希望这能让事情变得更清楚一点

输入:

['第一块文本\n 123我想要的统计数据(25%我想要的百分比)\n第二块文本\n 456我想要的第二个统计数据(50%我想要的第二个百分比)\n第三块文本\n 789我想要的第三个统计数据(75%第三个百分比)\n第四块文本\n 101第四个统计数据(100%第四个百分比)\n]

期望输出:

2第一行 您可以使用
split
获取前两行:

import re

data = ["Some text unsure how many numbers or if any special charectors etc. But I don't really care I just want all the text in this string \n 123 some more text (50% and some more text) \n"]

first_line, second_line = data[0].split("\n")[:2]
print first_line
# Some text unsure how many numbers or if any special charectors etc. But I don't really care I just want all the text in this string

digit_match = re.search('\d+(?![\d%])', second_line)
if digit_match:
    print digit_match.group()
    # 123

percent_match = re.search('\d+%', second_line)
if percent_match:
    print percent_match.group()
    # 50%
请注意,如果百分比写在其他数字之前,
\d+
将与百分比匹配(不带百分比)。我添加了一个,以确保匹配的数字后没有数字或
%

每一双 如果要继续分析行对,请执行以下操作:

data = [" The first chunk of text \n 123 the stats I want (25% the percentage I want) \n The Second chunk of text \n 456 the second stats I want (50% the second percentage I want) \n The third chunk of text \n 789 the third stats I want (75% the third percentage) \n The fourth chunk of text \n 101 The fourth stats (100% the fourth percentage) \n"]

import re

lines = data[0].strip().split("\n")

# TODO: Make sure there's an even number of lines
for i in range(0, len(lines), 2):
    first_line, second_line = lines[i:i + 2]

    print first_line

    digit_match = re.search('\d+(?![\d%])', second_line)
    if digit_match:
        print digit_match.group()

    percent_match = re.search('\d+%', second_line)
    if percent_match:
        print percent_match.group()
它输出:

The first chunk of text 
123
25%
 The Second chunk of text 
456
50%
 The third chunk of text 
789
75%
 The fourth chunk of text 
101
100%

\d+(%)++
中的括号是完全多余的。您真的打算允许超过1%的符号吗?只是出于好奇,如果您使用python,为什么需要正则表达式?你不想
yourstring.split('\n')[0]
做这个把戏吗?你想在右边之前\n做点什么吗?就这么做吧。*\n。还是我误解了你的要求?@mkingsbu我想知道偶数新行中的所有单词和奇数新行中的数字-如果这有意义的话?我明白了。是的,在这种情况下,正如其他人所提到的,我认为正则表达式是错误的方法。正则表达式本身必须包含所有逻辑,这可能很困难,具体取决于文件的大小。我不懂Python,但我知道如何在Bash中实现这一点。我将为循环创建一个C样式,并将变量读入其中。如果计数是偶数,那么我将把它附加到偶数矩阵,如果是奇数,我将把它附加到那里。也许你最好问一个稍微宽泛一点的问题,关于你想用这些数据做什么?谢谢你,非常感谢!有没有一种方法可以将其应用到不仅仅是第一个实例?也就是说,我的实际数据包含两个以上的字符串,我想用这个来处理所有这些字符串?@Maverick:已更新。谢谢你,非常感谢你的帮助!