From: David Masover Date: 2009-12-14T19:23:16+09:00 Subject: Re: problem with trivial regular expression On Monday 14 December 2009 04:06:08 am David Villa wrote: > fajljsfjaosfohttp://www.marca.comjafosjodfahttp://www.as.comjfoaasjofja Any particular context? Or is it actually that random? > i want to extract the diferents url but i try with : > /(http\:\/\/.+com)/ it returns a long match: > > 1. http://www.marca.comjafosjodfahttp://www.as.com If you think about it, that is still a valid URL. You're trying to limit it not to URLs, but only to http:// followed by a domain, and then only a domain ending in .com -- there are MANY urls that this will break. If you're OK with that, the basic problem is that . is going to match as much as it possibly can (greedy), and it matches any character. The simple solution is to make it match as few characters as it can (miserly). You do that by putting a question mark after the + or *: /(http\:\/\/.+?com)/ But again, that's not matching .com, that's matching anything ending in com. For example, on this URL: http://www.broadcom.com/ it will only capture http://www.broadcom. So there's an easy solution -- add an escaped dot: /(http\:\/\/.+?\.com)/ That's as much as I want to do with it. I'm guessing what you're trying to do is auto-linkify URLs in forum posts, or something like that -- some problem that's been solved a million times before, and better, so you should look for those solutions. But I won't assume that applies to you... By the way, if you don't already know: http://rubular.com/