Regular expression problem

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)

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 http://rubular.com/r/g5I6tYxb5C for where I show it works.

Thanks,
ben

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

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

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

nice one David!

Vladimir R. wrote:

nice one David!

I did with this

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

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
[email protected]