RegEx

J

Justin To

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!
 
W

Wayne Vucenic

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
 
M

Martin DeMello

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
 
M

Mikael Høilund

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-email+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
 
M

Martin DeMello

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
 
J

Justin To

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

Robert Klemme

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

Gareth Adams

Hi Justin,

Justin said:
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
 
J

Justin To

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!
 
P

prubel

Hi Justin,


Justin said:
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
 
D

David A. Black

Hi --

Hi Justin,


Justin said:
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.

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


David
 
W

Wayne Vucenic

Hi David,
And, in this case, the characters ( and ) :)

Great observation!

I hope to see you at RubyConf this year!

Best regards,

Wayne

Hi --

Hi Justin,


Justin said:
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.

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!
 
J

Justin To

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?
 
W

Wayne Vucenic

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
 
D

David A. Black

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
 

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

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top