RegEx

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!

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 V.
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 [email protected] 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

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

/^(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?

On Thu, Jun 19, 2008 at 3:48 PM, Mikael Høilund [email protected] wrote:

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

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

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: :wink:

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

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!

Hi Justin,

Try

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

Wayne

Hi –

On Sat, 21 Jun 2008, [email protected] wrote:

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.

And, in this case, the characters ( and ) :slight_smile:

David

Hi David,

And, in this case, the characters ( and ) :slight_smile:

Great observation!

I hope to see you at RubyConf this year!

Best regards,

Wayne

Wonderful help guys!

One other question related to that last bit:

match(/(v4.|v 4.|-4-/))

Would this also match the ‘(’ and ‘)’ ? Or are the parentheses grouping
the items in this case?

Hi Justin,

match(/(v4.|v 4.|-4-/))
Would this also match the ‘(’ and ‘)’ ? Or are the parentheses grouping
the items in this case?

Not to be picky, but I think you actually meant to place the
parentheses like this:

match(/(v4.|v 4.|-4-)/)

In any event, these would not match ‘(’ and ‘)’, but are just used for
grouping. In this particular case, you probably don’t need the
parentheses at all.

Also, note that ‘.’ matches any one character except a newline. In
context above, you might be expecting it to match a period, in which
case it should be ‘.’.

Wayne

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

Hi –

On Sat, 21 Jun 2008, Justin To wrote:

Wonderful help guys!

One other question related to that last bit:

match(/(v4.|v 4.|-4-/))

Would this also match the ‘(’ and ‘)’ ? Or are the parentheses grouping
the items in this case?

They’re grouping items. (And see Wayne’s correction.)

The hard thing about character classes:

/abc[def]/

is that the character class itself takes up horizontal space on your
screen, but it actually only represents one character in the string,
in the case a ‘d’, ‘e’, or ‘f’. It’s almost like a vertical construct,
like a slot machine:

 [d]

/abc[e]/
[f]

where only one of the values described in the character class is
actually going to match.

Of course, if you have a modifier like +:

/abc[def]+/

then the + means that the character class can match more than once.
But each time it matches, it’s still only consuming one character in
the string.

David