Regexp issue

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

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

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

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

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 :

http://www.regextester.com/
http://www.regular-expressions.info/reference.html
http://javascript.about.com/library/blre.htm

On Fri, Oct 8, 2010 at 8:40 AM, Idealone I. [email protected]
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

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

On 08.10.2010 16:55, [email protected] 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. :slight_smile:

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

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs