Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How do i match from these urls:
http://foo.bar.com http://bar.com
foo.bar from the first link and bar from the second link using one single regex?
foo.bar
bar
Try this:
^http://(.*)\.com$
Here's how to use it from Python:
import re for url in ['http://foo.bar.com', 'http://bar.com']: print re.match('^http://(.*)\.com$', url).group(1)
Output:
foo.bar bar
Add a comment
You didn't mention your language. In Ruby,
>> s = 'http://foo.bar.com' >> s =~ /\/\/(.*)\./ >> $1 => "foo.bar"
In Javascript, you will need to use the same regex as given by Mark Byers, no need to change anything:
var p = new RegExp("^http://(.*)\.com"); var out = p.exec(string_var_here); if(out != null) { var str = out[1]; }
Required, but never shown
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.
Explore related questions
See similar questions with these tags.