Python正则表达式从字符串中获取URL

标签 python regex

我使用正则表达式从字符串中获取 URL

这是我的代码:

import re

class ProcessWebsite(object):

    def __init__(self, *args, **kwargs):
        print "Test"

    def set_orden(self, command):
        self.command = command
        return self.getURL()

    def getURL(self):
        regex = re.compile("([0-9A-Za-z]{2,}\.[0-9A-Za-z]{2,3}\.[0-9A-Za-z]{2,3}|[0-9A-Za-z]{2,}\.[0-9A-Za-z]{2,3})$")
        return regex.findall(str(self.command))

我从这里调用方法“getURL”:

# -*- coding: utf-8 -*-
import Tkinter as tk
import tkFont
from Tkinter import StringVar
from Tkinter import Label
from processor.processwebsite import ProcessWebsite



def changeText(newText):
    var.set(newText)

def on_return_release(event):
    pr = ProcessWebsite()
    changeText(pr.set_orden(text.get("1.0", "end")))



app = tk.Tk()

var = StringVar()
label = Label(app, textvariable=var) 
font = tkFont.Font(family='Helvetica', size=36, weight='bold')
label.pack()

text = tk.Text(app, width=50, font=font)
text.config(width=35, height=2)
text.pack()
text.bind("<KeyRelease-Return>", on_return_release)

app.mainloop()

这会返回“None”,但是如果我更改方法“getURL”的这一行,那么它就可以工作

def getURL(self):
        regex = re.compile("([0-9A-Za-z]{2,}\.[0-9A-Za-z]{2,3}\.[0-9A-Za-z]{2,3}|[0-9A-Za-z]{2,}\.[0-9A-Za-z]{2,3})$")
        return regex.findall("Test with google.com")

有人知道会发生什么吗?

谢谢

最佳答案

文本选择似乎不适用于 .get(1.0, 'end'),这可能是由于在阅读之前添加了换行符。

我不太用Python,也许有更好的方法,但这很有效:

text.get('1.0', 'end -1 chars')

或者,仅获取第一行的文本:

text.get('1.0', '1.end')

关于Python正则表达式从字符串中获取URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118811/

相关文章:

python - 如何在python中设置文件所有者?

Python包:MLE for Dirichlet distribution

python - 无法对字体进行抗锯齿

python - 如何以立体方式显示 3D 渲染?

javascript - 迷失在翻译中 - C# 正则表达式到 JavaScript 正则表达式

python - 为什么 PLY 对待正则表达式与 Python/re 不同?

python - Python 新手,一直停留在这个问题上

c# - 在某个字符之前匹配任何内容的正则表达式?

python - 使用 re 在日志文件中查找字符串

python - 需要正则表达式 :(