# Regular Expressions for Leading white space

**URL:** <https://www.ruby-forum.com/t/regular-expressions-for-leading-white-space/159692>\
**Category:** Rails\
**Created:** [February 7, 2009, 6:17am UTC](https://www.ruby-forum.com/t/regular-expressions-for-leading-white-space/159692 "2009-02-07T06:17:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [February 7, 2009, 6:17am UTC](https://www.ruby-forum.com/t/regular-expressions-for-leading-white-space/159692/1 "2009-02-07T06:17:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [February 7, 2009, 6:33am UTC](https://www.ruby-forum.com/t/regular-expressions-for-leading-white-space/159692/2 "2009-02-07T06:33:34Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [February 7, 2009, 7:26am UTC](https://www.ruby-forum.com/t/regular-expressions-for-leading-white-space/159692/3 "2009-02-07T07:26:45Z")

</div>

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.
