2
import re

x = 'my website name is www.algoexpert.com and i have other website too'
for line in x:
    y = line.rstrip()
z = re.findall('.*\S+/.[a-z]{0-9}/.\S+', y) 
print(z) 

I just want to print the website name (www.algoexpert.com)

1
  • 2
    This is a bit broad. Do you just want www.something.com or do you need to support stuff like foo.something.tk? What constitutes a "website name"? Thanks for clarifying. Commented Oct 28, 2019 at 16:58

1 Answer 1

1

Issues to fix:

  • x is a string itself, why are you looping over it with for line in x?

  • [a-z]{0-9} - tries to cover only alphabetical chars, though in wrong way (could be {0,9}). The range of chars should be [a-z0-9]+ or at least - [a-z]+ (depending on the initial intention)

  • dots/periods . should be escaped with backslash \.

Fixed version (simplified):

import re

x = 'my website name is www.algoexpert.com and i have other website too'
z = re.findall('\S+\.[a-z0-9]+\.\S+', x.strip())
print(z)   # ['www.algoexpert.com']
Sign up to request clarification or add additional context in comments.

1 Comment

Actually x = open('file.txt'). I entered here wrong. That's why i looped it. I forget i gave it a string

Your Answer

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