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-forum.com/.
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-forum.com/.
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-forum.com/.
this seems to work:
/^(100(\.0{1,2})?|(\d{1,2})?(\.\d{1,2})?)$/
martin
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
Perhaps try /\d?\d(\.\d\d?)?|100(\.00?)?/ (untested).
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
-- 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
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
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 Thu, Jun 19, 2008 at 3:48 PM, Mikael Høilund <mikael@hoilund.org> wrote:
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).
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-forum.com/.
/^(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-forum.com/.
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.11It doesn't seem to match the 11.
Thanks so much!
--
Posted via http://www.ruby-forum.com/.
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
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
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?
Hi --
On Sat, 21 Jun 2008, prubel@bbn.com wrote:
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.11One 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 )
David
--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!
Hi David,
And, in this case, the characters ( and )
Great observation!
I hope to see you at RubyConf this year!
Best regards,
Wayne
On Fri, Jun 20, 2008 at 12:43 PM, David A. Black <dblack@rubypal.com> wrote:
Hi --
On Sat, 21 Jun 2008, prubel@bbn.com wrote:
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.11One 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 )
David
--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!
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?
--
Posted via http://www.ruby-forum.com/.
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 --
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
On Sat, 21 Jun 2008, Justin To wrote:
--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!