# Need some help with regular expression

**URL:** <https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636>\
**Category:** Rails\
**Created:** [August 12, 2011, 3:09pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636 "2011-08-12T15:09:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 12, 2011, 3:09pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636/1 "2011-08-12T15:09:42Z")

</div>

I need to validate a string from a user (a location for use with  
geotagging) that is. I want to make sure that the user specify a city  
and a country in my inp … So i wrote this =\>

validates :usri\_location,  
:presence =\> true,  
:format =\> {:with =\> /^[a-zA-Z]+{,\s}+[a-zA-Z]/i},  
:on =\> :create

I want to make sure that the user writes city, country

city + ", " + country

but all i get is (invalid) some one with more experience with regex  
wanting to help out or show me in a direction.  
(spent the last hour trying code from

> **[Regular Expressions Reference](https://www.regular-expressions.info/reference.html)**
>
> Reference of the various syntactic elements that can appear in regular expressions

but i can’t get it to work …

/Niklas.

---

<div class="post-metadata">

**Author:** ![Niklas\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@Niklas\_N](https://www.ruby-forum.com/u/Niklas_N)\
**Post date:** [August 12, 2011, 3:23pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636/2 "2011-08-12T15:23:19Z")

</div>

On 12 August 2011 14:08, Niklas N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> city + ", " + country
> 
> but all i get is (invalid) some one with more experience with regex  
> wanting to help out or show me in a direction.

The way I tackle this sort of problem is to start with a simple regex  
and slowly build it up. So you could start with just expecting alpha  
characters and check that it accepts the city ok, then add the  
expected comma, and so on.

Colin

---

<div class="post-metadata">

**Author:** ![Niklas\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@Niklas\_N](https://www.ruby-forum.com/u/Niklas_N)\
**Post date:** [August 12, 2011, 3:36pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636/3 "2011-08-12T15:36:19Z")

</div>

On Aug 12, 2011, at 9:21 AM, Colin L. wrote:

> On 12 August 2011 14:08, Niklas N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I need to validate a string from a user (a location for use with  
> > geotagging) that is. I want to make sure that the user specify a city  
> > and a country in my inp … So i wrote this =\>
> > 
> > validates :usri\_location,  
> > :presence =\> true,  
> > :format =\> {:with =\> /\[1\]+{,\s}+[a-zA-Z]/i},

You have {} instead of () around the , and \s. Also, I don’t believe  
the ‘,’ has to be escaped (it didn’t work with it escaped and it does if  
i remove the escaping. With those 2 issues fixed it works for me on  
rubular.

> and slowly build it up. So you could start with just expecting alpha

Juan Alvarado

* * *

1. a-zA-Z

---

<div class="post-metadata">

**Author:** ![Niklas\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@Niklas\_N](https://www.ruby-forum.com/u/Niklas_N)\
**Post date:** [August 12, 2011, 3:36pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636/4 "2011-08-12T15:36:56Z")

</div>

Try using [http://rubular.com/](http://rubular.com/). I use this a lot because it’s fast (most  
of the time) and interactive.

\*\*Leigh

---

<div class="post-metadata">

**Author:** ![Niklas\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@Niklas\_N](https://www.ruby-forum.com/u/Niklas_N)\
**Post date:** [August 12, 2011, 5:46pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636/5 "2011-08-12T17:46:59Z")

</div>

Thanks - this is a bookmark from now on…

---

<div class="post-metadata">

**Author:** ![Niklas\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@Niklas\_N](https://www.ruby-forum.com/u/Niklas_N)\
**Post date:** [August 12, 2011, 5:46pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636/6 "2011-08-12T17:46:59Z")

</div>

Thanks for all help!

---

<div class="post-metadata">

**Author:** ![Niklas\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@Niklas\_N](https://www.ruby-forum.com/u/Niklas_N)\
**Post date:** [August 12, 2011, 4:19pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636/7 "2011-08-12T16:19:18Z")

</div>

Juan Alvarado wrote in post #1016383:

> On Aug 12, 2011, at 9:21 AM, Colin L. wrote:
> 
> > On 12 August 2011 14:08, Niklas N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > I need to validate a string from a user (a location for use with  
> > > geotagging) that is. I want to make sure that the user specify a city  
> > > and a country in my inp … So i wrote this =\>
> > > 
> > > validates :usri\_location,  
> > > :presence =\> true,  
> > > :format =\> {:with =\> /\[1\]+{,\s}+[a-zA-Z]/i},
> 
> You have {} instead of () around the , and \s. Also, I don’t believe  
> the ‘,’ has to be escaped (it didn’t work with it escaped and it does if  
> i remove the escaping. With those 2 issues fixed it works for me on  
> rubular.

Also \s matches tabs and newlines, so your regex would match things  
like:

xxxx, yyyy

and

xxxx,  
yyy

so if you just want to match a space, then use a space in your regex:

/[a-zA-Z]+, [a-zA-z]+/

But then what if your user enters:

Los A., USA

or

San Jose, Costa Rica

* * *

1. a-zA-Z

---

<div class="post-metadata">

**Author:** ![Niklas\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@Niklas\_N](https://www.ruby-forum.com/u/Niklas_N)\
**Post date:** [August 12, 2011, 9:41pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636/8 "2011-08-12T21:41:54Z")

</div>

Just be aware that with that regular expression you are not allowing  
cities  
which names contains white spaces. New York, USA is not going to match.  
Also non standard ascii characters are not going to match (this could or  
could not be a problem depending on the language and city names for the  
countries you need to validate).

Apart of the regular expression, if you must be sure the input  
correspond to  
a “city, country” string you could also have cities and countries tables  
and  
check that the input is in your tables. Otherwise a string like “AAA,  
AAA”  
will match.

---

<div class="post-metadata">

**Author:** ![Niklas\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/cab0a1/32.png) [@Niklas\_N](https://www.ruby-forum.com/u/Niklas_N)\
**Post date:** [August 12, 2011, 5:47pm UTC](https://www.ruby-forum.com/t/need-some-help-with-regular-expression/210636/9 "2011-08-12T17:47:36Z")

</div>

Thanks 🙂
