# Regex help

**URL:** <https://www.ruby-forum.com/t/regex-help/170494>\
**Category:** Rails\
**Created:** [June 18, 2009, 5:46am UTC](https://www.ruby-forum.com/t/regex-help/170494 "2009-06-18T05:46:16Z")\
**Posts on this page:** 6\
**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:** [June 18, 2009, 5:46am UTC](https://www.ruby-forum.com/t/regex-help/170494/1 "2009-06-18T05:46:16Z")

</div>

Is there any regualar expressions,which Should not allow blank space as  
the first character for my Department Name field.  
any helps on this…  
Thanks

---

<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:** [June 18, 2009, 5:52am UTC](https://www.ruby-forum.com/t/regex-help/170494/2 "2009-06-18T05:52:08Z")

</div>

Newb N. wrote:

> Is there any regualar expressions,which Should not allow blank space as  
> the first character for my Department Name field.

Yes. Check out the regex documentation in the Pickaxe Book. Pay  
particular attention to the following constructs: [], \s, and ^ (in both  
its senses).

Or just use String#strip.

> any helps on this…  
> Thanks

You’re welcome!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<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:** [June 18, 2009, 9:28am UTC](https://www.ruby-forum.com/t/regex-help/170494/3 "2009-06-18T09:28:01Z")

</div>

2009/6/18 Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Is there any regualar expressions,which Should not allow blank space as  
> the first character for my Department Name field.  
> any helps on this…

If you just want to check that the first character of the string is  
not space (as opposed to stripping off leading whitespace) just check  
the first character using fieldname[0]. Depending on your  
requirements it may be more user friendly just to strip off whitespace  
using strip or strip!

Colin

---

<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:** [June 18, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/regex-help/170494/4 "2009-06-18T14:58:09Z")

</div>

Gavin M. wrote:  
[…]

> If you’re specifically looking for a regepxp that will match any thing  
> that doesn’t start with a space then try: /^[^\s]/

I was trying not to spoon-feed the lazy. 🙂

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<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:** [June 18, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/regex-help/170494/5 "2009-06-18T15:24:10Z")

</div>

Haha

Fair enough 🙂

Marnen Laibow-Koser wrote:

> Gavin M. wrote:  
> […]
> 
> > If you’re specifically looking for a regepxp that will match any thing  
> > that doesn’t start with a space then try: /^[^\s]/
> 
> I was trying not to spoon-feed the lazy. 🙂
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

---

<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:** [June 18, 2009, 12:34pm UTC](https://www.ruby-forum.com/t/regex-help/170494/6 "2009-06-18T12:34:42Z")

</div>

Newb N. wrote:

> Is there any regualar expressions,which Should not allow blank space as  
> the first character for my Department Name field.  
> any helps on this…  
> Thanks

If you’re specifically looking for a regepxp that will match any thing  
that doesn’t start with a space then try: /^[^\s]/

If it returns nil, there’s a space, if not then there’s no space.

Otherwise I’d go with Colin’s approach.

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com)**
>
> Ruby on Rails tips and tutorials
