"John Johnson" <jj5412 / earthlink.net> schrieb im Newsbeitrag
news:BB0E7D46.4D97%jj5412 / earthlink.net...
> While all the regexp experts have their ears perked up:
>
> Is there a way to match multiple words in a sentence in any order with
one
> regexp? I.e. I have a configuration file that looks like so:
>
> 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
>
> (Following Pragmatic Programmer's tips #20, 37, 52, 68, and 69 :-))
>
> I would like to extract the parameter names and values whether row is
before
> column (for instance), or someone wrote 'the number of lines on the
label
> are 12'. Perhaps I'm being a little to flexible here?

You can do

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

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

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

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

  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, words)
  extractNumericValues( line, *words.split( /\s+/ ) )
end


conf.each do |line|
  v = extractNumericValue line, "lines", "label"
  puts "label lines=#{v}" if v

  v = extractNumericValues line, "field", "piece", "name"
  puts "field piece name=#{v.inspect}" if v

  v = extractNumericValues2 line, "field piece description"
  puts "field piece description=#{v.inspect}" if v
end

Cheers

    robert