Ruby Regular expression to match a url
You can try this:
/https?:\/\/[\S]+/
The \S
means any non-whitespace character.
(Rubular)
What is the ruby regex to get a specific part of this URL?
str = "http://www.amazon.it/Calvin-Klein-Deluxe-K0S21120--Orologio/dp/B003CP0V6S/ref=lp_1597641031_1_8?ie=UTF8&qid=1349983393&sr=1-8"
(match = str.match(/\/dp\/([^\/]*)/)) && match[1]
# => "B003CP0V6S"
Ruby - Regular Expression to allow any Non-ASCII - Chinese characters in URL
You may use
/\A(?:https?:\/\/)?(?!www\.[^\/]*\z)[\d\p{L}_.-]+\.[\p{L}.]{2,6}(?:\/[^\s\/]+)*\/?\z/
See the regex demo
NOTE: To match a whole string with a Ruby regex, you should use \A
and \z
, not ^
and $
that match start and end of any line. That is why the demo is different from the final regex posted.
Pattern details
\A
- start of string(?:https?:\/\/)?
- an optionalhttp://
orhttps://
(?!www\.[^\/]*\z)
- the text that is immediately to the right cannot bewww.
and followed with any 0+ chars other than/
up to the end of the string[\d\p{L}_.-]+
- 1 or more digit, letter,_
,.
or-
chars\.
- a dot[\p{L}.]{2,6}
- 2 to 6 letters or dots(?:\/[^\s\/]+)*
- 0 or more sequences of/
and 1+ chars other than whitespace and/
\/?
- an optional/
\z
- end of string.
Regular Expression - matching file extension from a URL
Is your goal to get 'mp4'? Might consider not using a regex at all...
> require 'uri'
> uri = URI.parse('http://mtc.cdn.vine.co/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4?versionId=.k9_w6W7t1Yr1KUCWRIm6AnYhSdOUz32')
=> #<URI::HTTP http://mtc.cdn.vine.co/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4?versionId=.k9_w6W7t1Yr1KUCWRIm6AnYhSdOUz32>
> uri.path
=> "/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4"
> File.extname(uri.path)
=> ".mp4"
Regex to match rails get index urls
You may use
/\Ahttp:\/\/localhost:3000\/v2\/manufacturers(?:\/?(?:\?\S+)?|\/1\/models\/?)?\z/
See the Rubular demo
Pattern details
\A
- start of stringhttp:\/\/localhost:3000\/v2\/manufacturers
- ahttp://localhost:3000/v2/manufacturers
string(?:\/?(?:\?\S+)?|\/1\/models)?
- an optional sequence of:\/?
- an optional/
char(?:\?\S+)?
- an optional sequence of?
and 1+ non-whitespace|
- or\/1\/models\/?
-/1/models
string and an optional/
at the end
\z
- end of string.
Ruby Regex: Rejecting selected URLs
The following ruby code rejects all strings in the array containing cart
, order
or login
.
urls.reject { |url| url[/(cart|order|login)/] }
A raw regular expression which excludes words will use negative look-arounds:
^((?!login|order|cart).)*$
See rubular.
For more information, see @Max's suggested reading at Regular expression to match a line that doesn't contain a word?
Ruby on Rails URL Validation (regex)
Your input ( http://trentscott.com) does not have a subdomain but the regex is checking for one.
domain_regex = /^((http|https):\/\/)[a-z0-9]*(\.?[a-z0-9]+)\.[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$/ix
Update
You also don't need the ? after ((http|https):\/\/) unless the protocol is sometimes missing. I've also escaped . because that will match any character. I'm not sure what the grouping above is for, but here is a better version that supports dashes and groups by section
domain_regex = /^((http|https):\/\/)
(([a-z0-9-\.]*)\.)?
([a-z0-9-]+)\.
([a-z]{2,5})
(:[0-9]{1,5})?
(\/)?$/ix
Need a regular expression for match a 'url' in 'img' tag
You can use an expression like this one:<img.*?src=\\"(.+?)\\"
You'll need to use the global modifier for the regex to get all matches on several tags.
If you are testing one string at a time, it shouldn't be necessary.
Example at regex101
Related Topics
Why Can't I Install Rails on Lion Using Rvm
How to Set Up the Database.Yml File in Rails
Eoferror: End of File Reached Issue With Net::Http
Undefined Method Attr_Accessible
Rails: Skinny Controller Vs. Fat Model, or Should I Make My Controller Anorexic
How to Update Ruby to 1.9.X on Mac
Libmysqlclient.So.15: Cannot Open Shared Object File: No Such File or Directory
403 Forbidden on Rails App W/ Nginx, Passenger
How to Access Method Arguments in Ruby
Why Does Adding "Sleep 1" in an After Hook Cause This Rspec/Capybara Test to Pass
Class ≪≪ Self VS Self.Method With Ruby: What's Better
Ruby Local Variable Is Undefined