1

I'm using regular expressions to get a URL from a string

Here is my code:

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))

I call the method "getURL" from here:

# -*- 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()

This returns me "None", but if I change this line of the method "getURL" then it works

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")

Does anyone know what could be happening?

Thanks

3
  • Can you explain what it is supposed to do? What output should I test with and what should be the output? Commented Jun 14, 2013 at 23:58
  • It would appear that text.get("1.0", "end") is not returning what you expect it to. Can you confirm that this produces the desired output? Commented Jun 15, 2013 at 0:06
  • I want the domain output "google.com" in this case. Commented Jun 15, 2013 at 10:01

1 Answer 1

3

It would seem that the text selection doesn't work with .get(1.0, 'end'), which is probably due to the newline that was added before you read it.

I don't do Python much and maybe there's a much better approach, but this worked okay:

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

Or, to only get the text on the first line:

text.get('1.0', '1.end')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.