[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Justin To

6/19/2008 10:13:00 PM

I'm stuck trying to write a regular expression for a percentage:

Examples of what I'm trying to match:

1.1, 1.12, 12, 12.1 and 12.12

0.00 <= float <= 100.00


Thanks!
--
Posted via http://www.ruby-....

27 Answers

Wayne Vucenic

6/19/2008 10:30:00 PM

0

Hi Justin,

/(1?\d)?\d(\.\d\d?)?/

should be a good first approximation to what you're looking for. I
say "first approximation" because it will also match '100.99.'

Hope this helps!

Wayne

----
Wayne Vucenic
No Bugs Software
Agile Ruby and C# Contract Programming in Silicon Valley on Vista and OS X


On Thu, Jun 19, 2008 at 3:12 PM, Justin To <tekmc@hotmail.com> wrote:
> I'm stuck trying to write a regular expression for a percentage:
>
> Examples of what I'm trying to match:
>
> 1.1, 1.12, 12, 12.1 and 12.12
>
> 0.00 <= float <= 100.00
>
>
> Thanks!
> --
> Posted via http://www.ruby-....
>
>

Martin DeMello

6/19/2008 10:47:00 PM

0

On Thu, Jun 19, 2008 at 3:12 PM, Justin To <tekmc@hotmail.com> wrote:
> I'm stuck trying to write a regular expression for a percentage:
>
> Examples of what I'm trying to match:
>
> 1.1, 1.12, 12, 12.1 and 12.12
>
> 0.00 <= float <= 100.00

this seems to work:

/^(100(\.0{1,2})?|(\d{1,2})?(\.\d{1,2})?)$/

martin

Mikael Høilund

6/19/2008 10:49:00 PM

0


On Jun 20, 2008, at 0:12, Justin To wrote:

> I'm stuck trying to write a regular expression for a percentage:
>
> Examples of what I'm trying to match:
>
> 1.1, 1.12, 12, 12.1 and 12.12
>
> 0.00 <= float <= 100.00
>


Perhaps try /\d?\d(\.\d\d?)?|100(\.00?)?/ (untested).

--
a,b=%Q=Z,O^NPO\r4_PV\\PI\x15^-\x0\v=,email=%\%%%c\%115%%# Mikael
Hoilund, CTO
okay=%#;hmm=(0...a.size).map{|i|((a[i]-email[i]+2)%128).# of Meta.io
ApS from
chr}.join;!email.gsub!'o',"%c%c"%[3+?0.<<(2),?G.~@];aha=#############
Denmark
hmm.scan(/#{'(.)'*5}/);!puts(email[1..-12]+aha.shift.zip(*aha).join)#
Ruby <3


Martin DeMello

6/19/2008 11:01:00 PM

0

On Thu, Jun 19, 2008 at 3:48 PM, Mikael H=F8ilund <mikael@hoilund.org> wrot=
e:
>
> On Jun 20, 2008, at 0:12, Justin To wrote:
>
>> I'm stuck trying to write a regular expression for a percentage:
>>
>> Examples of what I'm trying to match:
>>
>> 1.1, 1.12, 12, 12.1 and 12.12
>>
>> 0.00 <=3D float <=3D 100.00
>>
>
>
> Perhaps try /\d?\d(\.\d\d?)?|100(\.00?)?/ (untested).

fails for 2100.0123 - you need to anchor with ^ and $ or at least \D and \D

also fails for .1 (though to be fair ruby rejects that too :))

martin

Justin To

6/19/2008 11:17:00 PM

0

/^(100(\.0{1,2})?|(\d{1,2})?(\.\d{1,2})?)$/

works Martin, but I made a mistake, I actually want the number at 0.00
to be just 0 and at 100.00 just 100... what alteration to your regex
would have to be made?
--
Posted via http://www.ruby-....

Robert Klemme

6/20/2008 6:22:00 AM

0

On 20.06.2008 01:16, Justin To wrote:
> /^(100(\.0{1,2})?|(\d{1,2})?(\.\d{1,2})?)$/
>
> works Martin, but I made a mistake, I actually want the number at 0.00
> to be just 0 and at 100.00 just 100... what alteration to your regex
> would have to be made?

Depending on the situation I'd probably just match floating point
numbers and check the converted value instead of creating a complex regexp.

/^\d{1,3}(?:\.\d{1,2})?$/

Kind regards

robert

Gareth Adams

6/20/2008 12:54:00 PM

0

Hi Justin,

Justin To wrote:
> I'm stuck trying to write a regular expression for a percentage:
>
> Examples of what I'm trying to match:
>
> 1.1, 1.12, 12, 12.1 and 12.12
>
> 0.00 <= float <= 100.00
>
>
> Thanks!

I hate to wheel out this overused quote, but meh: ;)

Some people, when confronted with a problem, think â??I know, Iâ??ll
use regular expressions.� Now they have two problems.
â??Jamie Zawinski, in comp.lang.emacs

(0..100).include? Float(perc_string) rescue false

irb(main):011:0* (0..100).include? Float("0") rescue false
=> true
irb(main):012:0> (0..100).include? Float("100") rescue false
=> true
irb(main):013:0> (0..100).include? Float("100.1") rescue false
=> false
irb(main):014:0> (0..100).include? Float("foo") rescue false
=> false
irb(main):015:0> (0..100).include? Float(".1") rescue false
=> true
irb(main):016:0> (0..100).include? Float("53.2") rescue false
=> true
irb(main):017:0> (0..100).include? Float("-5") rescue false
=> false


Justin To

6/20/2008 4:47:00 PM

0

Awesome, thanks for the help... I have one other regex question though,
pertaining to the same problem I'm trying to do

/^(\d+\.[(7)(11)])$/

That's my regex... it's suppose to match something like:

213823.7
or
848494223.11

It doesn't seem to match the 11.

Thanks so much!
--
Posted via http://www.ruby-....

Wayne Vucenic

6/20/2008 5:07:00 PM

0

Hi Justin,

Try

/^(\d+\.(7|11))$/

Wayne

On Fri, Jun 20, 2008 at 9:47 AM, Justin To <tekmc@hotmail.com> wrote:
> Awesome, thanks for the help... I have one other regex question though,
> pertaining to the same problem I'm trying to do
>
> /^(\d+\.[(7)(11)])$/
>
> That's my regex... it's suppose to match something like:
>
> 213823.7
> or
> 848494223.11
>
> It doesn't seem to match the 11.
>
> Thanks so much!
> --
> Posted via http://www.ruby-....
>
>

prubel

6/20/2008 5:10:00 PM

0


Hi Justin,


Justin To writes:
> Awesome, thanks for the help... I have one other regex question though,
> pertaining to the same problem I'm trying to do
>
> /^(\d+\.[(7)(11)])$/
>
> That's my regex... it's suppose to match something like:
>
> 213823.7
> or
> 848494223.11

One interesting thing to note is that it will match 7.1 but not 7.11.

What you need is some alteration, you want either 7 or 11. What you
have is 'choose from the set,' []. Where the set ends up containing
the characters 7 and 1. Since you match the decimal point and then one
character you'll never match on 11, which has 2.

> It doesn't seem to match the 11.

For the alteration the syntax looks like this: (a|b|cc) so try
something like this: /^(\d+\.(7|11))$/


Paul