例如,我有一个带有行的文件:
aaa$bb$ccc$ddd$eee
fff$ggg$hh$iii$jj
我需要获取$$内部的内容,因此预期的结果是:
$bb$
$ddd$
$ggg$
$iii$
我的结果是:
$bb$
$ggg$
我的解决方案:
m = re.search(r'$(.*?)$', line)
if m is not None:
print m.group(0)
有什么办法改进我的regexp吗?我试着使用*和+签名,但我不知道如何最终创建它。我在找类似的帖子,但找不到
我有这个程序(Python):
import re
line = "This is a phone number: (061) - 535555. And this is another: 01 - 4657289, 9846012345"
foundNum = re.search(r'((\(?\d{2,3}\)?\D{0,3}\d{6,10})|\d{10})', line)
print("The phone numbers found are: ", foundNum.groups())
我试图从“line”字符串(可能是另一个文本块
我正在尝试使用Python FileInput类编辑文本文件。首先,我将需要编写的代码行存储在字典中。然后遍历该字典,如果字典键与该行中的任何行匹配,则将该行替换为字典键-值对。如果文件中不存在dictionarykey,那么我希望在文件的末尾写入该行;但是,最后这部分不起作用,也没有写入文件。
下面是当前代码的样子:
def属性(file_edit,dst_path):
for key in properties.iterkeys():
for line in fileinput.FileInput(dst_path, inplace=1):
if str(key)
我想删除我的hrefs到我的数据集,但我得到这个错误:“不平衡的括号”!要删除"href",我使用以下python代码:
data = data.apply(lambda x: re.sub(re.findall(r'\<a(.*?)\>', x)[0], '', x) if (len(re.findall(r'\<a (.*?)\>', x))>0) and ('href' in re.findall(r'\<a (.*?)\>', x)[0]) else
我的re.findall搜索正在匹配并返回正确的字符串,但当我试图打印结果时,它会将其打印为列表而不是字符串。例子如下:
> line = ID=id5;Parent=rna1;Dbxref=GeneID:653635,Genbank:NR_024540.1,HGNC:38034;gbkey=misc_RNA;gene=WASH7P;product=WAS protein family homolog 7 pseudogene;transcript_id=NR_024540.1
> print re.findall(r'gene=[^;\n]+', line)
我有一个文件名列表,从中提取年份,然后将其传递给一个函数,以确定它是否是闰年。这些文件是电子表格,每个月都有一个工作表。然后,我查找字典来检索每个月的正确天数。我的函数本身工作得很好,但是如果我将正则表达式的结果提供给函数,那么函数就会崩溃,因为它们会得到一个列表项。我以为我可以简单地改变类型,但这不起作用。我已经包含了我的代码的一个精简版本,它说明了这个问题。尝试更改类型会在内部更改列表中的类型,但类仍然是一个列表,这会导致错误。
# Python 3
import re
def isleapyear(year):
if year % 4 == 0 and year % 100 !
我试图从一个字符串中得到一个RegExp的所有匹配,但是很明显,它在R中并不容易,或者我忽略了一些东西。说实话,这真的很让人困惑,我发现自己迷失在所有选择之中:str_extract、str_match、str_match_all、regexec、grep、gregexpr,还有谁知道还有多少其他的选项。
实际上,我所要做的只是简单地(在Python中):
>>> import re
>>> re.findall(r'([\w\']+|[.,;:?!])', 'This is starting to get really, r
我正在尝试获取一个文件中的所有链接。所有链接都包含在"“中,还有http、https和ftp链接。下面是文件的示例输出。
$ cat file
"http://www.google.com" and "http://www.yahoo.com" and "http://www.facebook.com"
"https://1.1.1.1" and "ftp://a.a.a.a"
下面的是我的python代码
In [109]: FILE = open('file','r'
我可能错过了一些非常基本的东西,但这里说:
我使用Python2.7和regex来识别字符串中的数字。
在控制台中,我输入:
>>> newstr = 'NukeNews/File_132.txt'
>>> int(re.findall(r'\d+',newstr)[0])
132
这正是我所期望的。
然而,在我正在运行的脚本中,我将字符串存储在一个名为linedict的字典中。我正在运行这个脚本:
news_id=[]
for line in line_vec:
print linedict[line]
n
我正在使用python脚本在文本文件中运行行。我想在文本文档中搜索一个img标记,并将标记作为文本返回。
当我运行regex re.match(line)时,它返回一个_sre.SRE_MATCH对象。如何让它返回字符串?
import sys
import string
import re
f = open("sample.txt", 'r' )
l = open('writetest.txt', 'w')
count = 1
for line in f:
line = line.rstrip()
imgt
直到这个函数,一切都好,我得到4999行,这是我得到的数量。你能检查下面的代码吗?在哪里我会犯错误,我最终得到的是5095而不是4999;在第二个函数中,我有5032而不是4999个实例。
我只能拿到4999英镑。任何帮助都是非常感谢的。
a=[]
for i in matches:
a.append([i for i in list(dict.fromkeys(i))])
print(len(a))
print ((a))
结果:
4999
[['23-year-old'], [' '], ['42 years old'], ['
我想从这句话中提取the cat和another mat: >>> text = "the cat sat on another mat"
>>>
>>> re.findall('(the|another)\s+\w+', text)
['the', 'another'] 但是它不会返回后面的cat和mat。如果我把它改成re.findall('another\s+\w+', text),它就会找到那个部分,但是为什么(first thing | secon
我认为我在RegEx方面已经足够好了,我可以读到任何一个,但是这个简单的(用Python)让我很困惑。www.regexpal.com给出的结果与iPython不同。
data = 'four year entrepreneurial program. Students develop and run a business, gain much needed ...'
m = re.compile('entrepreneur|business\s(plan|model)')
m.findall(data)
给出['']
这怎么可能是对的?如果
我正在编写一个脚本,以获取扫描的pdf文件,并将它们转换成行文本输入数据库。我使用re.findall从一个正则表达式列表中获取匹配,以从所提取的tesseract字符串中获得某些值。当正则表达式找不到匹配时,我会遇到麻烦,我希望它返回“Error”。所以我看出来有个问题。
我尝试了几个if/ get语句,但似乎没有注意到None值。
from wand.image import Image as Img
import ghostscript
from PIL import Image
import pytesseract
import re
import os
def get_text_f
我需要在python中使用regex来提取和标记之间的文本。
示例:Customizable:<strong>Features Windows 10 Pro</strong> and legacy ports <b>including VGA,</b> HDMI, RJ-45, USB Type A connections.
为此,我正在做以下工作:
pattern=re.compile("(<b>(.*?)</b>)|(<strong>(.*?)</strong>)")
for l
我是python的新手,我试图从文本文件中只提取日期,但当我匹配它们时,结果就是这样。我能得到关于如何摆脱这些的帮助吗?我的代码:
import re
file1 = open('wireshark.txt', 'r')
for line in file1:
regex = re.match(r"\d\d\d\d-\d\d-\d\d", line)
marks = re.findall(r"\d\d\d\d-\d\d-\d\d", line)
print(regex, marks)
代码执行输出:
N
所以我正在学习python,我根本无法理解正则表达式在这个中是如何工作的。
我试着阅读了文档,我理解了方法是如何工作的,但是我不知道for循环是如何在其中适用的。
import re
lookAndSay = "1"
lookAndSay = "".join([str(len(j) + 1) + i for i, j in re.findall(r"(\d)(\1*)", lookAndSay)])
print(LookAndSay)
输出是
11