tags:

views:

69

answers:

3

I'm trying to add conditional logic to determine if there's one regex match for a URL in a string. Here's an example of the string:

string_to_match = "http://www.twitpic.com/23456 ran to catch the bus, http://www.twitpic.com/3456 dodged a bullet at work."

I only want to match if I determine there's one URL in the string, so the above string wouldn't be a match in the case I'm trying to solve. I thought something like this would work:

if string_to_match =~  /[http\:\/\/]?/
   puts "you're matching more then once. bad man!"
end

But it doesn't! How do I determine that there's only one match in a string?

+1  A: 

you could do it like this:

if string_to_match =~ /((http:\/\/.*?)http:\/\/)+/

this would match only if you have 2 (or more) occurrences of http://

ennuikiller
ennuikiller, I'm looking to write conditional logic for a single match of 'http://' in a string, not more then one match. I think my phrasing might have been confusing. Thanks for responding all the same!
aressidi
to reverse the sense of the match simply replace =~ with !=
ennuikiller
+1  A: 

Take a look at String#scan, you can use it this way:

if string_to_match.scan(/[http\:\/\/]/).count > 1
   puts "you're matching more then once. bad man!"
end
Mladen Jablanović
Thanks Mladen! This is what worked for me: string_to_match.scan(/(http\:\/\/)/).length == 1. You need to group with () otherwise each individual character that matches is inserted into the array.
aressidi
You're right, sorry, I haven't tested the regex, just pasted the one from your question.
Mladen Jablanović
+2  A: 

The answer from Mladen is fine (counting the return from scan), but regular expressions already include the idea of matching the same thing multiple times or a particular number of times. In your case, you want to print the warning if your text occurs 2 or more times:

/(http:\/\/.+?){2,}/

Use .+ or .*, depending on whether you want to require the URL to have some content or not. As it stands, the .+? will match 1 or more characters in a non-greedy fashion, which is what you want. A greedy quantifier would gobble up the entire string on the first try and then have to do a bunch of backtracking before ultimately finding multiple URLs.

FM