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.
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.
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/projects/rmagick/\]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618\]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html\]
"foo 01-02" =~ /(\d+)-(\d+)/
puts $1 #=> "01"
puts $2 #=> "02"
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.
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
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.
--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
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"
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.
On Wed, May 30, 2007 at 10:40:09PM +0900, Ken Bloom wrote:
Others have suggested the following correction:
/(\d+)-?(\d+)/
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
On Wed, May 30, 2007 at 08:15:05AM +0900, bcparanj@gmail.com wrote:
The regexp: /foo\ (\d+)\-?(\d*)/
seems to work. Still testing...