# Regexp issue

**URL:** <https://www.ruby-forum.com/t/regexp-issue/196185>\
**Category:** Ruby\
**Created:** [October 8, 2010, 8:40am UTC](https://www.ruby-forum.com/t/regexp-issue/196185 "2010-10-08T08:40:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [October 8, 2010, 8:40am UTC](https://www.ruby-forum.com/t/regexp-issue/196185/1 "2010-10-08T08:40:52Z")

</div>

Hi All,  
I have an issue in parsing regular expression and im new to  
regexp.  
Varaiable = “Internation tip ($25 fee)”  
The $25 might vary each time, it might be $5, $ 10 so on

I tried doing something like this which didnt work, kindly help  
variable = “Internation tip (regexp:$[0-9][0-9]+ fee)”  
i want the regular expression to aid both single digit or multi digit  
currency.

Cheers

---

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [October 8, 2010, 8:53am UTC](https://www.ruby-forum.com/t/regexp-issue/196185/2 "2010-10-08T08:53:41Z")

</div>

On Friday 08 October 2010, Idealone I. wrote:

> |  
> |Cheers

The $, ( and ) characters have special meaning in a regexp:

- $ means the end of the line
- ( opens a group
- ) closes a group  
To match a literal $, ( or ) you have to escape them with a backslash.  
This  
should work:

reg = /Internation tip ($\d{1,2} fee)/

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [October 8, 2010, 10:19am UTC](https://www.ruby-forum.com/t/regexp-issue/196185/3 "2010-10-08T10:19:46Z")

</div>

On 10-10-08 02:53 AM, Stefano C. wrote:

> > |currency.  
> > reg = /Internation tip ($\d{1,2} fee)/
> 
> I hope this helps
> 
> Stefano

if you just want to match the currency including the brackets, the  
following reg-ex should do:

/($\d+.\*)/

where

.\* means match zero or more characters  
the ‘.’ matches any single character  
the ‘\*’ mean zero or more

---

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [October 8, 2010, 10:36am UTC](https://www.ruby-forum.com/t/regexp-issue/196185/4 "2010-10-08T10:36:53Z")

</div>

Idealone I. wrote:

> Hi All,  
> I have an issue in parsing regular expression and im new to  
> regexp.  
> Varaiable = “Internation tip ($25 fee)”  
> The $25 might vary each time, it might be $5, $ 10 so on
> 
> I tried doing something like this which didnt work, kindly help  
> variable = “Internation tip (regexp:$[0-9][0-9]+ fee)”  
> i want the regular expression to aid both single digit or multi digit  
> currency.
> 
> Cheers

As Stefano C. pointed out $,(,) must be escaped.  
To allow any number of digits, and at least 1 use \d{1,).  
Moreover you must match different descriptions and check for null  
values.  
Example:

var =[“Internation tip1 ($9 fee)”,“Internation tip2 ($25 fee)”,  
“Internation tip3 ($350 fee)”,“Internation tip3 ($5000 fee)”,  
“Internation tip5”]

rx =/.+(($_\d{1,}\s+fee))._/i

fmt= “%-30s %6s\n”  
var.each do |str|  
if( m=rx.match(str) )  
printf fmt , str, m[1] # m[1]=first sub-expression  
else printf fmt, str,‘Null Value’  
end #if  
end #do

HTH gfb

---

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [October 8, 2010, 4:56pm UTC](https://www.ruby-forum.com/t/regexp-issue/196185/5 "2010-10-08T16:56:39Z")

</div>

I think what you would want is :

$[0-9]{1,} fee

‘$’ says that you are looking for the ‘$’ character not the end of the  
line…which is what I think you are looking for. Then you have the  
[0-9]{1,} which says that you are looking for at least one number, but  
that there could be more. This should work for you, but you can also  
check out these links :

> **[Regex Tester and Debugger Online - Javascript, PCRE, PHP](https://www.regextester.com/)**
>
> Regular Expression Tester with highlighting for Javascript and PCRE. Quickly test and debug your regex.

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

> **[Find out Why You Should Use JavaScript to Enhance Your Website](https://www.thoughtco.com/why-javascript-2037560)**
>
> Find out how you should use JavaScript so that it enhances your website with the many reasons why you may want to use the program.

---

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [October 8, 2010, 11:07am UTC](https://www.ruby-forum.com/t/regexp-issue/196185/6 "2010-10-08T11:07:46Z")

</div>

On Fri, Oct 8, 2010 at 8:40 AM, Idealone I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi All,  
> I have an issue in parsing regular expression and im new to  
> regexp.  
> Varaiable = “Internation tip ($25 fee)”

First of all, “Variable” is a constant. You need to make the name  
start with a lowercase letter.

> The $25 might vary each time, it might be $5, $ 10 so on
> 
> I tried doing something like this which didnt work, kindly help  
> variable = “Internation tip (regexp:$[0-9][0-9]+ fee)”  
> i want the regular expression to aid both single digit or multi digit  
> currency.

There is a simple approach and a more robust approach:

irb(main):008:0\> v = “Internation tip ($25 fee)”  
=\> “Internation tip ($25 fee)”  
irb(main):009:0\> v[/\d+/].to\_i  
=\> 25  
irb(main):010:0\> v[/\AInternation tip ($(\d+) fee)\z/, 1].to\_i  
=\> 25

The first regexp extracts any number, i.e. this works with differently  
formatted strings as long as there is at least one digit somewhere.  
The second one checks that your string actually follows the pattern  
you described.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [October 8, 2010, 7:02pm UTC](https://www.ruby-forum.com/t/regexp-issue/196185/7 "2010-10-08T19:02:10Z")

</div>

unknown wrote:

> [0-9]{1,} which says that you are looking for at least one number, but  
> that there could be more.

\d+ is a more concise way to write that.

\d =\> digit [0-9]

- suffix =\> 0 or more instances

- suffix =\> 1 or more instances  
? suffix =\> 0 or 1 instances

---

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [October 8, 2010, 7:10pm UTC](https://www.ruby-forum.com/t/regexp-issue/196185/8 "2010-10-08T19:10:42Z")

</div>

On 08.10.2010 16:55, removed\_email\_address@domain.invalid wrote:

> I think what you would want is :
> 
> $[0-9]{1,} fee
> 
> ‘$’ says that you are looking for the ‘$’ character not the end of  
> the line…which is what I think you are looking for. Then you have  
> the [0-9]{1,} which says that you are looking for at least one  
> number, but that there could be more. This should work for you, but  
> you can also check out these links :

Instead of [0-9] you can type \d and instead of {1,} you can type + -  
much less hassle. 🙂

And then of course you also need a capturing group to get at the digits.

Cheers

robert
