Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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,我试图从python中的字符串中提取一个子字符串 我的数据文件包含古兰经的一行,其中每一行在字符串的开头都标有韵文和章节号。 我想尝试提取第一个数字和第二个数字,并将它们写入另一个文本文件中的一行 下面是txt文件中几行的示例 2|12|Of a surety, they are the ones who make mischief, but they realise (it) not. 2|242|Thus doth Allah Make clear His Signs to you: In

我试图从python中的字符串中提取一个子字符串

我的数据文件包含古兰经的一行,其中每一行在字符串的开头都标有韵文和章节号。 我想尝试提取第一个数字和第二个数字,并将它们写入另一个文本文件中的一行 下面是txt文件中几行的示例

2|12|Of a surety, they are the ones who make mischief, but they realise (it) not.
2|242|Thus doth Allah Make clear His Signs to you: In order that ye may understand.
正如您所看到的,韵文和章节可能包含多个数字,因此仅从字符串开始计算空格数是不够的。 有没有办法使用正则表达式将第一个数字(韵文)和第二个数字(章节)提取为字符串

我写这篇文章的代码将尝试将韵文和章节字符串写入Arff文件。 arff文件中的一行示例如下:

1,0,0,0,0,0,0,0,0,2,12
其中,最后两个值为诗句和章节

下面是for循环,它将为每一节写我感兴趣的属性,然后我想尝试通过使用正则表达式提取每一行的相关子字符串,将每一节和每一章写到最后

for line in verses:
    for item in topten:
        count = line.count(item)
        ARFF_FILE.write(str(count) + ",")
    # Here is where i could use regular expressions to extract the desired substring 
    # verse and chapter then write these to the end of a line in the arff file.
    ARFF_FILE.write("\n")
我认为章号(管道前的第一个数字)的正则表达式应该是这样的,然后使用group(0)函数来获取第一个数字和

"^(\d+)\|(\d)\|" 
然后,第(1)组应获得韵文的regexp

但我不知道如何在python中实现这一点。 有人有什么想法吗? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 对问题的回答

我刚刚尝试实现您的技术,但得到一个“索引错误:列表索引超出范围。我的代码是

for line in verses:
 for item in topten:
     parts = line.split('|')

     count = line.count(item)
     ARFF_FILE.write(str(count) + ",")
 ARFF_FILE.write(parts[0] + ",")
 ARFF_FILE.write(parts[1])  
 ARFF_FILE.write("\n")

带括号?这不是所有正则表达式的工作方式吗?

如果所有行的格式都像
A | B | C
,那么您不需要任何正则表达式,只需拆分它即可

for line in fp:
    parts = line.split('|') # or line.split('|', 2) if the last part can contain |
    # use parts[0], parts[1]

我认为最简单的方法是使用re.split()来获取经文 和一个re.findall()来获取章节和诗句的编号 结果将存储在列表中,以便以后使用 下面是一个代码示例:

#!/usr/bin/env python

import re

# string to be parsed
Quran= '''2|12|Of a surety, they are the ones who make mischief, but they realise (it) not.
2|242|Thus doth Allah Make clear His Signs to you: In order that ye may understand.'''

# list containing the text of all the verses
verses=re.split(r'[0-9]+\|[0-9]+\|',Quran)
verses.remove("")

# list containing the chapter and verse number:
#
#   if you look closely, the regex should be r'[0-9]+\|[0-9]+\|'
#   i ommited the last pipe character so that later when you need to split
#   the string to get the chapter and verse nembuer you wont have an
#   empty string at the end of the list
#
chapter_verse=re.findall(r'[0-9]+\|[0-9]+',Quran)


# looping over the text of the verses assuming len(verses)==len(chp_vrs)
for index in range(len(verses)):
    chapterNumber,verseNumber =chapter_verse[index].split("|")
    print "Chapter :",chapterNumber, "\tVerse :",verseNumber
    print verses[index]

但是我已经在语料库中使用for循环了,所以拆分它并不是一个选项。@user680466:恐怕我不明白你的意思。我不是说要在那里抛出另一个循环,我是说你的循环应该进行拆分。这个注释框不允许我正确地编写代码,所以我会添加另一个答复。请阅读。嗨,我补充道d我在我的“问题”@user680466中的回答:在内部循环之前或之后移动
parts=line.split(…)
,否则
parts
将不存在于外部范围中。忘记我之前说过的话,我刚刚尝试实现您的技术,但得到一个“索引错误:列表索引超出范围”。我的代码是针对第行的:对于topten中的项目:parts=line.split(“|”)count=line.count(item)ARFF_FILE.write(str(count)+“,”)ARFF_FILE.write(parts[0])ARFF_FILE.write(parts[1])ARFF_FILE.write(“\n”)topten是什么?您不会在发布的代码中的任何地方实例化它。一般来说,不清楚你的输入是什么,你想要的输出是什么。