In my app, I allow users to submit urls. They (of course) need the ability to submit urls with a forward slash, "/", but whats the regular expression to allow them to do that? I currently use: validates_format_of :url, :with => /^[-\w\_.]+$/i to only allow alphanumerics, dashes, underscores, and dots to prevent cross site scripting when I later reconstruct these urls, but I can''t figure out how to allow "/" as well. Any ideas? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
maryam kamali
2009-Dec-19 07:31 UTC
Re: Regular expression: How do I allow forward slashes?
inotwell cumpiuter &and lunguish engilish thanks On Sat, Dec 19, 2009 at 10:31 AM, AlwaysCharging <goodgets-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In my app, I allow users to submit urls. They (of course) need the > ability to submit urls with a forward slash, "/", but whats the > regular expression to allow them to do that? > > I currently use: > > validates_format_of :url, :with => /^[-\w\_.]+$/i > > to only allow alphanumerics, dashes, underscores, and dots to prevent > cross site scripting when I later reconstruct these urls, but I can''t > figure out how to allow "/" as well. > > Any ideas? > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 18, 11:01 pm, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In my app, I allow users to submit urls. They (of course) need the > ability to submit urls with a forward slash, "/", but whats the > regular expression to allow them to do that? > > I currently use: > > validates_format_of :url, :with => /^[-\w\_.]+$/i > > to only allow alphanumerics, dashes, underscores, and dots to prevent > cross site scripting when I later reconstruct these urls, but I can''t > figure out how to allow "/" as well. > > Any ideas?am a newbee.. but i think u can use underscore method for the whole url that will put ''/'' instead of '' : '' -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Have you tried escaping them "\/"? On Dec 18, 11:01 pm, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In my app, I allow users to submit urls. They (of course) need the > ability to submit urls with a forward slash, "/", but whats the > regular expression to allow them to do that? > > I currently use: > > validates_format_of :url, :with => /^[-\w\_.]+$/i > > to only allow alphanumerics, dashes, underscores, and dots to prevent > cross site scripting when I later reconstruct these urls, but I can''t > figure out how to allow "/" as well. > > Any ideas?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sven Riedel
2009-Dec-19 09:14 UTC
Re: Re: Regular expression: How do I allow forward slashes?
frogstarr78 wrote:> Have you tried escaping them "\/"?Another way would be to use %r, that way you can avoid the leaning toothpick syndrome alltogether; /^http:\/\/myhostname\.com\/foo$/i would become %r{http://myhostname\.com/foo}i But before you start piecing your own regexp together have a look at the regexp patterns in the URI::REGEXP::PATTERN module (in your ruby lib directory under uri/common.rb). Could save you some work depending on what and how you want to validate. Sven -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sven Riedel
2009-Dec-19 09:20 UTC
Re: Re: Regular expression: How do I allow forward slashes?
Sven Riedel wrote:> /^http:\/\/myhostname\.com\/foo$/i > > would become > > %r{http://myhostname\.com/foo}iAnd of course I forgot the anchors in the second example. So the correct version is: %r{^http://myhostname\.com/foo$}i -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Use Ruby''s other regexp syntax: %r{pattern} To continue your example below: validates_format_of :url, :with => %r{^[-\w_./]+$} AlwaysCharging wrote:> In my app, I allow users to submit urls. They (of course) need the > ability to submit urls with a forward slash, "/", but whats the > regular expression to allow them to do that? > > I currently use: > > validates_format_of :url, :with => /^[-\w\_.]+$/i-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
AlwaysCharging
2009-Dec-19 18:12 UTC
Re: Regular expression: How do I allow forward slashes?
Yes, that did it. Thank you. No idea how I try everything and overlook the simplest solution, duh. And, Thank you to everyone else that weighed in as well, definitely some other options to look into. Side note: Anybody know why the period doesn''t have to be escaped? Like just "." allows the dot to be input, as well as "\." So, [-\w\_\.\/] works just as [-\w\_.\/]. Why is this? On Dec 19, 3:41 am, frogstarr78 <frogstar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Have you tried escaping them "\/"? > > On Dec 18, 11:01 pm, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > In my app, I allow users to submit urls. They (of course) need the > > ability to submit urls with a forward slash, "/", but whats the > > regular expression to allow them to do that? > > > I currently use: > > > validates_format_of :url, :with => /^[-\w\_.]+$/i > > > to only allow alphanumerics, dashes, underscores, and dots to prevent > > cross site scripting when I later reconstruct these urls, but I can''t > > figure out how to allow "/" as well. > > > Any ideas?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
It actually depends on where the "." is in the Regexp. In your case it is inside a Character Class "[]". So it is matching the "." character explicitly. Since \w is shorthand for the [a-zA-Z] character class. It is parsed as a character class instead of an escaped "w" character. So you could actually change the character class to be %r|[-\w_./]|. No need to further escape the "_", or "-" since it is at the beginning of the class (That''s for another reason though). On Dec 19, 10:12 am, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Yes, that did it. Thank you. > No idea how I try everything and overlook the simplest solution, duh. > > And, Thank you to everyone else that weighed in as well, definitely > some other options to look into. > > Side note: Anybody know why the period doesn''t have to be escaped? > Like just "." allows the dot to be input, as well as "\." > So, [-\w\_\.\/] works just as [-\w\_.\/]. Why is this? > > On Dec 19, 3:41 am, frogstarr78 <frogstar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Have you tried escaping them "\/"? > > > On Dec 18, 11:01 pm, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > In my app, I allow users to submit urls. They (of course) need the > > > ability to submit urls with a forward slash, "/", but whats the > > > regular expression to allow them to do that? > > > > I currently use: > > > > validates_format_of :url, :with => /^[-\w\_.]+$/i > > > > to only allow alphanumerics, dashes, underscores, and dots to prevent > > > cross site scripting when I later reconstruct these urls, but I can''t > > > figure out how to allow "/" as well. > > > > Any ideas?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
maryam kamali
2009-Dec-20 06:46 UTC
Re: Re: Regular expression: How do I allow forward slashes?
hello iam maryam ihave peroblem ishal go aftenon by On Sat, Dec 19, 2009 at 11:21 AM, ralu <ralu.rm-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Dec 18, 11:01 pm, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > In my app, I allow users to submit urls. They (of course) need the > > ability to submit urls with a forward slash, "/", but whats the > > regular expression to allow them to do that? > > > > I currently use: > > > > validates_format_of :url, :with => /^[-\w\_.]+$/i > > > > to only allow alphanumerics, dashes, underscores, and dots to prevent > > cross site scripting when I later reconstruct these urls, but I can''t > > figure out how to allow "/" as well. > > > > Any ideas? > > > am a newbee.. but i think u can use underscore method for the whole > url that will put ''/'' instead of '' : '' > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
AlwaysCharging
2009-Dec-20 07:10 UTC
Re: Regular expression: How do I allow forward slashes?
But that made me question why I couldn''t just put the "/" inside of the bracket as well. Like why did that have to be escaped if the period didn''t. (I guess it''s because in that syntax, the forward slash has closure properties.) Oh well it''s working now, and I escaped the . as well (\.). Thank you for your help. Much appreciated frogstarr On Dec 20, 12:09 am, frogstarr78 <frogstar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> It actually depends on where the "." is in the Regexp. In your case it > is inside a Character Class "[]". So it is matching the "." character > explicitly. Since \w is shorthand for the [a-zA-Z] character class. It > is parsed as a character class instead of an escaped "w" character. So > you could actually change the character class to be %r|[-\w_./]|. No > need to further escape the "_", or "-" since it is at the beginning of > the class (That''s for another reason though). > > On Dec 19, 10:12 am, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Yes, that did it. Thank you. > > No idea how I try everything and overlook the simplest solution, duh. > > > And, Thank you to everyone else that weighed in as well, definitely > > some other options to look into. > > > Side note: Anybody know why the period doesn''t have to be escaped? > > Like just "." allows the dot to be input, as well as "\." > > So, [-\w\_\.\/] works just as [-\w\_.\/]. Why is this? > > > On Dec 19, 3:41 am, frogstarr78 <frogstar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Have you tried escaping them "\/"? > > > > On Dec 18, 11:01 pm, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > In my app, I allow users to submit urls. They (of course) need the > > > > ability to submit urls with a forward slash, "/", but whats the > > > > regular expression to allow them to do that? > > > > > I currently use: > > > > > validates_format_of :url, :with => /^[-\w\_.]+$/i > > > > > to only allow alphanumerics, dashes, underscores, and dots to prevent > > > > cross site scripting when I later reconstruct these urls, but I can''t > > > > figure out how to allow "/" as well. > > > > > Any ideas?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yeah, I understand what you mean. No worries. On Dec 19, 11:10 pm, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> But that made me question why I couldn''t just put the "/" inside of > the bracket as well. Like why did that have to be escaped if the > period didn''t. (I guess it''s because in that syntax, the forward > slash has closure properties.) > Oh well it''s working now, and I escaped the . as well (\.). > Thank you for your help. Much appreciated frogstarr > > On Dec 20, 12:09 am, frogstarr78 <frogstar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > It actually depends on where the "." is in the Regexp. In your case it > > is inside a Character Class "[]". So it is matching the "." character > > explicitly. Since \w is shorthand for the [a-zA-Z] character class. It > > is parsed as a character class instead of an escaped "w" character. So > > you could actually change the character class to be %r|[-\w_./]|. No > > need to further escape the "_", or "-" since it is at the beginning of > > the class (That''s for another reason though). > > > On Dec 19, 10:12 am, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Yes, that did it. Thank you. > > > No idea how I try everything and overlook the simplest solution, duh. > > > > And, Thank you to everyone else that weighed in as well, definitely > > > some other options to look into. > > > > Side note: Anybody know why the period doesn''t have to be escaped? > > > Like just "." allows the dot to be input, as well as "\." > > > So, [-\w\_\.\/] works just as [-\w\_.\/]. Why is this? > > > > On Dec 19, 3:41 am, frogstarr78 <frogstar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Have you tried escaping them "\/"? > > > > > On Dec 18, 11:01 pm, AlwaysCharging <goodg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > In my app, I allow users to submit urls. They (of course) need the > > > > > ability to submit urls with a forward slash, "/", but whats the > > > > > regular expression to allow them to do that? > > > > > > I currently use: > > > > > > validates_format_of :url, :with => /^[-\w\_.]+$/i > > > > > > to only allow alphanumerics, dashes, underscores, and dots to prevent > > > > > cross site scripting when I later reconstruct these urls, but I can''t > > > > > figure out how to allow "/" as well. > > > > > > Any ideas?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.