"John Johnson" <jj5412 / earthlink.net> schrieb im Newsbeitrag
news:BB0F29BC.4DEC%jj5412 / earthlink.net...
> on 6/13/03 6:32 AM, Robert Klemme at bob.news / gmx.net wrote:
> I have a function I'm using now has_both(string, regexp1, regexp2) which
I
> use in an if statement, then I go back and extract the value(s). I was
> hoping there was a RegExp that matched two words so I could put
everything
> into a nice case/when statement.

You can do that:

conf=<<CONF
label lines 12
the label is 40 characters wide
print field piece name at row 2 column 23
print field piece description at column 13 and row 3
CONF

class CaseElem
  def initialize(*words)
    @words=words
  end

  def ===(line)
    @words.each do |word|
      rx = Regexp.new "\\b#{word}\\b"
      return false unless rx.match line
    end

  end

end

def extractNumericValue(line)
  m = /\b(-?\d+)\b/.match line
  return m && m[1]
end

def extractNumericValues(line)
  result = Hash.new

  line.scan /\b(\w+)\s+(-?\d+)\b/ do |m|
    result[ m[0] ]= m[1]
  end

  return !result.empty? && result
end


def extractNumericValues2(line)
  extractNumericValues( line )
end

# constant
CaseOne = CaseElem.new "field", "piece", "name"

conf.each do |line|
  case line
    when CaseOne
      v = extractNumericValue line
      puts v if v
  end
end


> I suppose there isn't, though.

Well, you can do that, but you need to have n! alternatives for n words:

conf.each do |line|
  if ( m =
/(\blines\b.*\blabel\b|\blabel\b.*\blines\b).*\b(\d+)\b/.match( line ) )
    puts "label lines=#{m[2]}"
  end
end

I'm not sure which of the two variants (one regexp vs. n regexps) is more
efficient.  I guess you will hit some limit if n becomes larger.

> Thanks again!

You're welcome.

Regards

    robert