# RegEx

**URL:** <https://rubytalk.org/t/regex/47242>\
**Category:** ruby-talk\
**Created:** [19 June 2008 22:12 UTC](https://rubytalk.org/t/regex/47242 "2008-06-19T22:12:52Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin\_To](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Justin\_To](https://rubytalk.org/u/Justin_To)\
**Post date:** [19 June 2008 22:12 UTC](https://rubytalk.org/t/regex/47242/1 "2008-06-19T22:12:52Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wayne\_Vucenic1](https://avatars.discourse-cdn.com/v4/letter/w/f17d59/32.png) [@Wayne\_Vucenic1](https://rubytalk.org/u/Wayne_Vucenic1)\
**Post date:** [19 June 2008 22:30 UTC](https://rubytalk.org/t/regex/47242/2 "2008-06-19T22:30:26Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [19 June 2008 22:46 UTC](https://rubytalk.org/t/regex/47242/3 "2008-06-19T22:46:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mikael\_Hoilund](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Mikael\_Hoilund](https://rubytalk.org/u/Mikael_Hoilund)\
**Post date:** [19 June 2008 22:48 UTC](https://rubytalk.org/t/regex/47242/4 "2008-06-19T22:48:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gareth\_Adams](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Gareth\_Adams](https://rubytalk.org/u/Gareth_Adams)\
**Post date:** [20 June 2008 12:53 UTC](https://rubytalk.org/t/regex/47242/5 "2008-06-20T12:53:31Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;Some people, when confronted with a problem, think “I know, I’ll  
&nbsp;&nbsp;&nbsp;&nbsp;use regular expressions.” Now they have two problems.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;—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

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [19 June 2008 23:01 UTC](https://rubytalk.org/t/regex/47242/6 "2008-06-19T23:01:11Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Justin\_To](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Justin\_To](https://rubytalk.org/u/Justin_To)\
**Post date:** [20 June 2008 16:47 UTC](https://rubytalk.org/t/regex/47242/7 "2008-06-20T16:47:04Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Justin\_To](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Justin\_To](https://rubytalk.org/u/Justin_To)\
**Post date:** [19 June 2008 23:16 UTC](https://rubytalk.org/t/regex/47242/8 "2008-06-19T23:16:37Z")

</div>

/^(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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wayne\_Vucenic1](https://avatars.discourse-cdn.com/v4/letter/w/f17d59/32.png) [@Wayne\_Vucenic1](https://rubytalk.org/u/Wayne_Vucenic1)\
**Post date:** [20 June 2008 17:07 UTC](https://rubytalk.org/t/regex/47242/9 "2008-06-20T17:07:09Z")

</div>

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-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Paul\_Rubel](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@Paul\_Rubel](https://rubytalk.org/u/Paul_Rubel)\
**Post date:** [20 June 2008 17:10 UTC](https://rubytalk.org/t/regex/47242/10 "2008-06-20T17:10:13Z")

</div>

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))$/

&nbsp;&nbsp;&nbsp;&nbsp;Paul

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [20 June 2008 06:23 UTC](https://rubytalk.org/t/regex/47242/11 "2008-06-20T06:23:36Z")

</div>

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

&nbsp;&nbsp;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?

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [20 June 2008 19:43 UTC](https://rubytalk.org/t/regex/47242/12 "2008-06-20T19:43:35Z")

</div>

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.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:  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS July 21-24 Edison, NJ  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Wayne\_Vucenic1](https://avatars.discourse-cdn.com/v4/letter/w/f17d59/32.png) [@Wayne\_Vucenic1](https://rubytalk.org/u/Wayne_Vucenic1)\
**Post date:** [20 June 2008 20:00 UTC](https://rubytalk.org/t/regex/47242/13 "2008-06-20T20:00:51Z")

</div>

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.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](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Justin\_To](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Justin\_To](https://rubytalk.org/u/Justin_To)\
**Post date:** [20 June 2008 22:28 UTC](https://rubytalk.org/t/regex/47242/14 "2008-06-20T22:28:15Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wayne\_Vucenic1](https://avatars.discourse-cdn.com/v4/letter/w/f17d59/32.png) [@Wayne\_Vucenic1](https://rubytalk.org/u/Wayne_Vucenic1)\
**Post date:** [20 June 2008 23:10 UTC](https://rubytalk.org/t/regex/47242/15 "2008-06-20T23:10:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [21 June 2008 07:50 UTC](https://rubytalk.org/t/regex/47242/16 "2008-06-21T07:50:41Z")

</div>

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:

&nbsp;&nbsp;&nbsp;/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:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[d]  
/abc[e]/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[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 +:

&nbsp;&nbsp;&nbsp;/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:  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS July 21-24 Edison, NJ  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
