# Regular expression problem

**URL:** <https://www.ruby-forum.com/t/regular-expression-problem/187648>\
**Category:** Rails\
**Created:** [April 27, 2010, 11:47am UTC](https://www.ruby-forum.com/t/regular-expression-problem/187648 "2010-04-27T11:47:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hunt](https://www.ruby-forum.com/u/hunt)\
**Post date:** [April 27, 2010, 11:47am UTC](https://www.ruby-forum.com/t/regular-expression-problem/187648/1 "2010-04-27T11:47:06Z")

</div>

## var text=“Afganistan (+86)” var code=text.sub(/\w+/, ‘’) result: code = (+86)

var text = “Antigua and Barbuda (+1268)”  
var code=text.sub(/\w+/, ‘’)  
result : code = and Barbuda (+1268)"

* * *

what regular expression I can try to get second one as first. ie (+1268)

---

<div class="post-metadata">

**Author:** ![hunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hunt](https://www.ruby-forum.com/u/hunt)\
**Post date:** [April 27, 2010, 12:16pm UTC](https://www.ruby-forum.com/t/regular-expression-problem/187648/2 "2010-04-27T12:16:58Z")

</div>

HUNT HUNT wrote:

> ## var text=“Afganistan (+86)” var code=text.sub(/\w+/, ‘’) result: code = (+86)
> 
> var text = “Antigua and Barbuda (+1268)”  
> var code=text.sub(/\w+/, ‘’)  
> result : code = and Barbuda (+1268)"
> 
> * * *
> 
> what regular expression I can try to get second one as first. ie (+1268)

Do you want “(+1268)” from the second query? If so, does

var code=text.sub(/[\w+]/, ‘’)

work? See [Rubular: [\w+]](http://rubular.com/r/g5I6tYxb5C) for where I show it works.

Thanks,  
ben

---

<div class="post-metadata">

**Author:** ![hunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hunt](https://www.ruby-forum.com/u/hunt)\
**Post date:** [April 27, 2010, 12:30pm UTC](https://www.ruby-forum.com/t/regular-expression-problem/187648/3 "2010-04-27T12:30:33Z")

</div>

“Antigua and Barbuda (+1268)”.scan(/\d+/).to\_s  
=\> 1268

“Antigua and Barbuda (+1268)”.scan(/(+\d+)/).to\_s  
=\> (+1268)

---

<div class="post-metadata">

**Author:** ![hunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hunt](https://www.ruby-forum.com/u/hunt)\
**Post date:** [April 27, 2010, 12:35pm UTC](https://www.ruby-forum.com/t/regular-expression-problem/187648/4 "2010-04-27T12:35:14Z")

</div>

Hi –  
On Tue, 27 Apr 2010, Vladimir R. wrote:

> “Antigua and Barbuda (+1268)”.scan(/\d+/).to\_s  
> =\> 1268
> 
> “Antigua and Barbuda (+1268)”.scan(/(+\d+)/).to\_s  
> =\> (+1268)

There’s a nice technique for quickly getting a substring from a  
string using a subscript-style notation:

“Antigua and Barbuda (+1268)”[/(+\d+)/] # “(+1268)”

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally  
COMPLEAT Coming to Chicago area, June 18-19, 2010!  
RUBYIST [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![hunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hunt](https://www.ruby-forum.com/u/hunt)\
**Post date:** [April 27, 2010, 12:45pm UTC](https://www.ruby-forum.com/t/regular-expression-problem/187648/5 "2010-04-27T12:45:47Z")

</div>

nice one David!

---

<div class="post-metadata">

**Author:** ![hunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hunt](https://www.ruby-forum.com/u/hunt)\
**Post date:** [April 27, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/regular-expression-problem/187648/6 "2010-04-27T13:01:32Z")

</div>

Vladimir R. wrote:

> nice one David!

I did with this

var code=text.sub(/[a-z A-Z]/, ‘’)

---

<div class="post-metadata">

**Author:** ![hunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hunt](https://www.ruby-forum.com/u/hunt)\
**Post date:** [April 27, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/regular-expression-problem/187648/7 "2010-04-27T16:31:18Z")

</div>

David A. Black wrote:

> Hi –  
> On Tue, 27 Apr 2010, Vladimir R. wrote:
> 
> > “Antigua and Barbuda (+1268)”.scan(/\d+/).to\_s  
> > =\> 1268
> > 
> > “Antigua and Barbuda (+1268)”.scan(/(+\d+)/).to\_s  
> > =\> (+1268)
> 
> There’s a nice technique for quickly getting a substring from a  
> string using a subscript-style notation:
> 
> “Antigua and Barbuda (+1268)”[/(+\d+)/] # “(+1268)”

Ooh, nice! Didn’t know about that.

> David

## Best,

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