From: Hai anh Le Date: 2008-11-28T00:24:42+09:00 Subject: Re: reg exp Brian Candler wrote: > Hai anh Le wrote: >> Hi every body. I'm a newbie in Ruby and have problem with reg exp. >> >> In one string as example "The device 13c include something as in Fig. 2a >> and 3b", I want to detect some numbers have style "13c" and ignore >> others number have style "2a and 3b" >> >> I use /(\d+[a-z]*)( [^a][^n][^d] )(\d)+[a-z]*/ but it not ok. Can >> someone help me plz? > > Tricky. I suggest: > > str.scan(/\d+[a-z]?(?: and \d+[a-z]?)?/).reject { |e| /and/ =~ e } > > I thought about lookahead assertions, e.g. (?! and \d+), but since these > don't consume characters the 3b is likely to be matched later as a > standalone number. > > Details at http://www.ruby-doc.org/docs/ProgrammingRuby/ - click on "The > Ruby Language" in the contents, and scroll down to Extensions. Hi Brian! I have another example as: "The coefficients are then mixed with the data from mixer 51xy, so that the recording equipment 14abc stores the six channel data stream, the scripting data 26aa and 27ac" My purpose is to find the position of number have style "123abc", after that I get the string from begin to that position like "The coefficients are then mixed with the data from mixer 51xy," Then I will continued search... My code like while string.size do if string=~ /.../ then temp_string = $` + $& string.gsub!(temp_string, "") else break end So that what I need is $& = "123abc" and not = "123abc and 123abc" -- Posted via http://www.ruby-forum.com/.