Regular Expressions for Leading white space


#1

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.


#2

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


#3

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.