Said Kaldybaev
Said Kaldybaev

Reputation: 9952

regex any non-digit with exception

I've got strings like these:

+996999966966AA
-996999966966AA

I am using this code:

"+996999966966AA".gsub!(/\D/, "")

to get rid of any character except digits, but the sign + also being stripped. How can my code retain the +?

Upvotes: 6

Views: 5277

Answers (2)

JstRoRR
JstRoRR

Reputation: 3693

You can also use \W, "non-word character" which matches any character that is not a word character (alphanumeric & underscore)).

(\W\d+)\w+

Upvotes: 1

Barmar
Barmar

Reputation: 780909

Use:

[^+\d]

to match anything that isn't + or a digit.

Upvotes: 14

Related Questions