[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Regexp to extract number with hyphen

bcparanj@gmail.com

5/29/2007 10:46:00 PM

I have a text with "foo 01-02" in a string. I want to extract foo
01-02 using regexp. reg = /foo (\d+)/ only extracts foo with numbers
when there is no hyphen.

reg = /foo (\d{1,})|(\-)|(\d{1,})/ only extracts foo 01. TIA.

6 Answers

Tim Hunter

5/29/2007 10:56:00 PM

0

bcparanj@gmail.com wrote:
> I have a text with "foo 01-02" in a string. I want to extract foo
> 01-02 using regexp. reg = /foo (\d+)/ only extracts foo with numbers
> when there is no hyphen.
>
> reg = /foo (\d{1,})|(\-)|(\d{1,})/ only extracts foo 01. TIA.
>
>

Does this work?

/foo (\d+-\d+)/

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


Tim Pease

5/29/2007 10:58:00 PM

0

On 5/29/07, bcparanj@gmail.com <bcparanj@gmail.com> wrote:
> I have a text with "foo 01-02" in a string. I want to extract foo
> 01-02 using regexp. reg = /foo (\d+)/ only extracts foo with numbers
> when there is no hyphen.
>
> reg = /foo (\d{1,})|(\-)|(\d{1,})/ only extracts foo 01. TIA.
>

"foo 01-02" =~ /(\d+)-(\d+)/

puts $1 #=> "01"
puts $2 #=> "02"

bcparanj@gmail.com

5/29/2007 11:11:00 PM

0

The regexp: /foo\ (\d+)\-?(\d*)/
seems to work. Still testing...
On May 29, 3:57 pm, "Tim Pease" <tim.pe...@gmail.com> wrote:
> On 5/29/07, bcpar...@gmail.com <bcpar...@gmail.com> wrote:
>
> > I have a text with "foo 01-02" in a string. I want to extract foo
> > 01-02 using regexp. reg = /foo (\d+)/ only extracts foo with numbers
> > when there is no hyphen.
>
> > reg = /foo (\d{1,})|(\-)|(\d{1,})/ only extracts foo 01. TIA.
>
> "foo 01-02" =~ /(\d+)-(\d+)/
>
> puts $1 #=> "01"
> puts $2 #=> "02"


Ken Bloom

5/30/2007 3:36:00 AM

0

On Tue, 29 May 2007 15:45:34 -0700, bcparanj@gmail.com wrote:

> I have a text with "foo 01-02" in a string. I want to extract foo 01-02
> using regexp. reg = /foo (\d+)/ only extracts foo with numbers when
> there is no hyphen.
>
> reg = /foo (\d{1,})|(\-)|(\d{1,})/ only extracts foo 01. TIA.

That's because the | operator in a regexp means either-or. So you're
matching
foo (\d{1,})
or you're matching
(\-)
or you're matching
(\d{1,})
but not all three at the same time.
Others have suggested the following correction:
/(\d+)-?(\d+)/

--Ken


--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Brian Candler

5/30/2007 7:32:00 AM

0

On Wed, May 30, 2007 at 08:15:05AM +0900, bcparanj@gmail.com wrote:
> The regexp: /foo\ (\d+)\-?(\d*)/
> seems to work. Still testing...

What exactly are the allowed matches? Only num1-num2, or is num1 by itself
allowed? What about num1-num2-num3?

/foo ([0-9-]+)/ matches .....foo 1-2-3.... and foo 1-2 and foo 1
/foo (\d+-\d+)/ matches .....foo 1-2.... only
/foo (\d+(-\d+)?)/ matches .....foo 1-2.... and foo 1, but not foo-1-2-3

Brian Candler

5/30/2007 1:56:00 PM

0

On Wed, May 30, 2007 at 10:40:09PM +0900, Ken Bloom wrote:
> Others have suggested the following correction:
> /(\d+)-?(\d+)/

That particular example matches 11, but not 1.

The most important thing is to be clear about exactly what you want to allow
to match or not match; then writing the regexp is easy.