Forum: Ruby on Rails Regular Expressions for Leading white space

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Newb N. (Guest)
on 2009-02-07 07:17
Can Anyone help me with regular expression which checks the string, if
it contains Leading whitespaces.
example
=> admin
it contains leading space with it.
so i need to check it.
pls help me.
Philip H. (Guest)
on 2009-02-07 07:33
(Received via mailing list)
> Can Anyone help me with regular expression which checks the string, if
> it contains Leading whitespaces.
> example
> => admin
> it contains leading space with it.
> so i need to check it.
> pls help me.

Read up on the Regex class and pattern matching in general...

str = " some string with a leading space"

if str =~ /^\s/
   puts "we have a leading space"
else "
   puts "we don't"
end
Mohit S. (Guest)
on 2009-02-07 08:26
(Received via mailing list)
Philip H. wrote:
>
> str = " some string with a leading space"
>
> if str =~ /^\s/
>    puts "we have a leading space"
> else "
>    puts "we don't"
> end
>
Not directly related, but if you just need to remove the leading/
trailing spaces, you could use lstrip, rstrip and strip on strings.

Cheers,
Mohit.
2/7/2009 | 2:25 PM.
This topic is locked and can not be replied to.