From: Ken Bloom Date: 2007-05-30T22:40:09+09:00 Subject: Re: Regexp to extract number with hyphen 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/~kbloom1/