regexp issue

  • Thread starter Idealone Ideally
  • Start date
I

Idealone Ideally

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
 
S

Stefano Crocco

|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

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
 
R

Rajinder Yadav

|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

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

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
 
G

Gianfranco Bozzetti

Idealone said:
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 Crocco 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
 
R

Robert Klemme

Hi All,
=A0 =A0 =A0 =A0I have an issue in parsing regular expression and im new t= o
regexp.
=A0Varaiable =3D "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 =3D "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 =3D "Internation tip ($25 fee)"
=3D> "Internation tip ($25 fee)"
irb(main):009:0> v[/\d+/].to_i
=3D> 25
irb(main):010:0> v[/\AInternation tip \(\$(\d+) fee\)\z/, 1].to_i
=3D> 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


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top