Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
使用Python3.x计算与特定正则表达式模式匹配的行数_Python_Regex_Python 3.x - Fatal编程技术网

使用Python3.x计算与特定正则表达式模式匹配的行数

使用Python3.x计算与特定正则表达式模式匹配的行数,python,regex,python-3.x,Python,Regex,Python 3.x,我有一个源UTF8文件(无BOM表,windows EOL),看起来如下所示: ~someunicodetext_someunicodetext_someunicodetext~ some_more_unicode_text_some_more_unicode_text ~someunicodetext_someunicodetext_someunicodetext~ some_more_unicode_text_some_more_unicode_text &&even_mo

我有一个源UTF8文件(无BOM表,windows EOL),看起来如下所示:

~someunicodetext_someunicodetext_someunicodetext~
some_more_unicode_text_some_more_unicode_text

~someunicodetext_someunicodetext_someunicodetext~
some_more_unicode_text_some_more_unicode_text
&&even_more_text_here

~someunicodetext_someunicodetext_someunicodetext~
some_more_unicode_text_some_more_unicode_text

~someunicodetext_someunicodetext_someunicodetext~
import re, codecs
pattern = re.compile(r'some_expression_here')
count = 0
with codecs.open("some_input_file", "r", "UTF8") as inputFile:
    inputFile=inputFile.read()
    lines = re.findall(pattern, inputFile)
    for match in lines:
        count +=1
print (count)
~someunicodetext_someunicodetext_someunicodetext~

some_more_unicode_text_some_more_unicode_text



~someunicodetext_someunicodetext_someunicodetext~

some_more_unicode_text_some_more_unicode_text

&&even_more_text_here
因此,有3种类型的行(如果计算空行数,则为4)。我的目标是使用python正则表达式计算每个非空的类型。这绝对必须是使用Python3.x的基于正则表达式的解决方案,因为我想了解它是如何工作的

我的python脚本如下所示:

~someunicodetext_someunicodetext_someunicodetext~
some_more_unicode_text_some_more_unicode_text

~someunicodetext_someunicodetext_someunicodetext~
some_more_unicode_text_some_more_unicode_text
&&even_more_text_here

~someunicodetext_someunicodetext_someunicodetext~
some_more_unicode_text_some_more_unicode_text

~someunicodetext_someunicodetext_someunicodetext~
import re, codecs
pattern = re.compile(r'some_expression_here')
count = 0
with codecs.open("some_input_file", "r", "UTF8") as inputFile:
    inputFile=inputFile.read()
    lines = re.findall(pattern, inputFile)
    for match in lines:
        count +=1
print (count)
~someunicodetext_someunicodetext_someunicodetext~

some_more_unicode_text_some_more_unicode_text



~someunicodetext_someunicodetext_someunicodetext~

some_more_unicode_text_some_more_unicode_text

&&even_more_text_here
我遇到的真正问题是实际的正则表达式。
~.*
似乎能够匹配上面示例中的1、4、8等行(如果我们从1开始计算)
&.
与第6行匹配
但我不知道如何计算未标记的行,即第2、5、9行。
在记事本++中,这个表达式
^(?(~.*)|(&&&&.*).
或者简单地说就是这个
^(?(!~&)。*
对我有效(尽管它不完全正确),但我在python中复制它的所有尝试都失败了

编辑
inputFile.read()
没有按我期望的方式读取文件(hello windows EOL)。哪一个可能重要,也可能不重要。它的输出如下所示:

~someunicodetext_someunicodetext_someunicodetext~
some_more_unicode_text_some_more_unicode_text

~someunicodetext_someunicodetext_someunicodetext~
some_more_unicode_text_some_more_unicode_text
&&even_more_text_here

~someunicodetext_someunicodetext_someunicodetext~
some_more_unicode_text_some_more_unicode_text

~someunicodetext_someunicodetext_someunicodetext~
import re, codecs
pattern = re.compile(r'some_expression_here')
count = 0
with codecs.open("some_input_file", "r", "UTF8") as inputFile:
    inputFile=inputFile.read()
    lines = re.findall(pattern, inputFile)
    for match in lines:
        count +=1
print (count)
~someunicodetext_someunicodetext_someunicodetext~

some_more_unicode_text_some_more_unicode_text



~someunicodetext_someunicodetext_someunicodetext~

some_more_unicode_text_some_more_unicode_text

&&even_more_text_here

您可以使用re.MULTILINE标志“”尝试此模式

re.UNICODE
标志也应用于Python2

下面是一个完整的示例:

import re, codecs

with codecs.open("input.txt", "r", "UTF8") as inputFile:
    data = inputFile.read()
pattern = re.compile(r'^\w.*', flags=re.MULTILINE)
lines = re.findall(pattern, data)

>>> data   #  note windows line termination
'~someunicodetext_someunicodetext_someunicodetext~\r\nsome_more_unicode_text_some_more_unicode_text\r\n   \t\r\n~someunicodetext_someunicodetext_someunicodetext~\r\nsome_more_unicode_text_some_more_unicode_text\r\n&&even_more_text_here\r\n\r\n~someunicodetext_someunicodetext_someunicodetext~\r\nsome_more_unicode_text_some_more_unicode_text\r\n\r\n~someunicodetext_someunicodetext_someunicodetext~\r\n'

>>> print(lines)
['some_more_unicode_text_some_more_unicode_text\r', 'some_more_unicode_text_some_more_unicode_text\r', 'some_more_unicode_text_some_more_unicode_text\r']

>>> print(len(lines))
3

因此,正则表达式根据需要匹配“未标记”的非空行。

答案如下。我仍然不确定我是否正确处理了windows EOL等等,但这似乎是可行的。 我也有点希望有人能回答我的问题,解释一下我的问题在哪里,以及为什么它是这样运作的,但是哦,好吧

这是怎么回事。我们匹配前面有~EOL且以另一个EOL结尾的每一行。同时,我们确保排除具有2个或更多连续EOL的匹配

所以。这仅与标记为的行正下方的行匹配~

import re, codecs

regex = re.compile(r'(?!~(\r\n){2,})~\r\n.*\r\n', re.MULTILINE)
count = 0

with codecs.open('input_file', 'r', 'UTF8') as inputFile:
    inputFile=inputFile.read()
    lines = re.findall(regex, inputFile)
    for match in lines:
        count +=1
print (count)
“未标记”行可以识别为不平淡、不以
~
开头且不以
&
开头的行

因此,以下正则表达式将起作用:

^[^&\s].*

阅读:
^
=开头匹配,
[^…]
=不在中的单个字符,
&\s
=字符
&
或空白字符(即不是其中之一),
*
=之后可以出现任何字符

(我把
\s
放进去以防万一,因为你说你的换行有问题。我不确定是否需要)

而且,逐行读取文件要好得多。你会得到:

import re, codecs
pattern = re.compile(r'^[^&\s].*')
with codecs.open("some_input_file", "r", "UTF8") as inputFile:
    count = sum( 1 for line in inputFile if re.search(pattern, line) )
print (count)

这给出了不包括空格的所有行的计数。所以空行不会被计数。希望这有帮助。

我的意思是,如果从1开始计数,它与我的示例中的第1、4、8行匹配。不是内部逻辑之类的。除非你每次都加上,否则第1行、第4行和第8行就是没有标记的行。我不明白你的意思。我测试了它,它完全符合我说的。Count返回正确的结果,这才是重要的。我尝试过,但我相信它也会计算空行。如果我在last for循环下添加
print(match)
,我确实会看到很多空白,添加/删除空行会更改结果计数。还忘了提到,
^\w.
不会产生正确的结果。它只返回从普通字母开始的行,而不返回一些关于我的记事本++示例的奇怪符号(但不返回空行)。npp中的
*
将匹配任何非空行。也许这就是为什么它对我有用。我认为python不是这样的。对不起,我误解了你的要求。。。我认为每种类型的线路都需要单独计数。我的答案是一个正则表达式模式来匹配那些不以~或&开头的非空行。是的,我理解。这就是问题所在<代码>^[^~&].$也与空行匹配。空行没有
~
&
,但它们有开头
^
、结尾
$
和中间的内容
*
运行它时的输出是什么?我得到“3”-它似乎只匹配空白行。此外,for循环计数器可以替换为
len(lines)
。3是正确答案。我还解释了它与什么匹配。谢谢你的循环提示。当你目不转睛地看它时,你可能会看到垃圾,但它确实起到了作用?您可以看到输入文件包含3个空行和3个未标记行。你到底在匹配哪一个?尝试打印
,您将看到[u'',u'',u''],即3个空行。我需要计算直接位于~中所含行下方的行数,此答案就是这样!(问题本身已经给出了计算所有其他行数的解决方案。)无需大喊大叫。在你最初的问题中,它在哪里说的?你说,非常笨拙,你需要计算未标记的行(那些不以~或&开头的行)。你不是说你需要数一数由~-括起的一行后面的行,而是说没有标记的行。无论如何,如果你认为你的答案是正确的,就去用它。当您的输入文件更改时,享受调试它的乐趣。Nope。首先,这在Python3.x中根本不起作用。其次,这不处理输入文件,正确处理EOL和其他内容。第三,以我的实际示例作为输入字符串,结果是“8”行,而正确答案是“3”。结果应该是8 rt……。所有非空行……。如果我错了,请纠正我。。。。。。对于输入文件,也要做一些小的修改……我想可以马上合并……结果应该是三个。你能看到搜索“每一个非空白行”和该行的“每一个非空白类型”之间的区别吗。总共有3种类型,我们正在搜索其中一种!你没有正确地解释你想要什么,这就是为什么我们对你的问题都有困难。上述内容在Python3中确实有效,但print语句不起作用,