Question on regexp

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?

ADVthanksANCE!

···


Regards,
JJ

Finally using a Mac!

“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

“John Johnson” jj5412@earthlink.net wrote in message
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:

Yes: if you want to match row and column with numeric value

/(row\s+\d+).(column\s+\d+)|(column\s+\d+).(row\s+\d+)/

I haven’t testet this, but that is the general idea. And it is not a good
way to do it because of backtracking in the regular expression engine, but
might not matter in your case.

It’s better to control parsing logic at a higher level: search for row or
column first:
/row|column\s+\d+/, then repeat.

Mikkel

Well, you could do something like this:

re = /(column|row) (\d+)/i
“print field pieve name at row 2 column 23”.scan(re)

=> [[“row”, “2”], [“column”, “23”]]

“print field piece description at column 13 and row 3”.scan(re)

=> [[“column”, “13”], [“row”, “3”]]

ri String#scan for more details on scan.

But I’m not exactly sure if this is what you’re trying to do. You might be
better off with a full-blown parser: I’ve never used any of the Ruby parser
packages (Or any parser package for that matter) so I don’t how hard that
would be.

Jason Creighton

···

On Fri, 13 Jun 2003 07:56:21 +0900 John Johnson jj5412@earthlink.net wrote:

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?

ADVthanksANCE!

“John Johnson” jj5412@earthlink.net schrieb im Newsbeitrag

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:

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

Thanks Robert, for taking the time to code all that.

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. I suppose there isn’t, though.

Thanks again!

···

on 6/13/03 6:32 AM, Robert Klemme at bob.news@gmx.net wrote:


Regards,
JJ

Be Kind, Be Careful, Be Yourself

That’s interesting (your regexps above), I’ll give that a whirl.

Thanks!

···

on 6/13/03 6:18 PM, Jason Creighton at androflux@remove.to.reply.softhome.net wrote:

On Fri, 13 Jun 2003 07:56:21 +0900 > John Johnson jj5412@earthlink.net wrote:

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?

ADVthanksANCE!

Well, you could do something like this:

re = /(column|row) (\d+)/i
“print field pieve name at row 2 column 23”.scan(re)

=> [[“row”, “2”], [“column”, “23”]]

“print field piece description at column 13 and row 3”.scan(re)

=> [[“column”, “13”], [“row”, “3”]]

ri String#scan for more details on scan.

But I’m not exactly sure if this is what you’re trying to do. You might be
better off with a full-blown parser: I’ve never used any of the Ruby parser
packages (Or any parser package for that matter) so I don’t how hard that
would be.

Jason Creighton


Regards,
JJ

Finally using a Mac!

“John Johnson” jj5412@earthlink.net schrieb im Newsbeitrag
news:BB0F29BC.4DEC%jj5412@earthlink.net…

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
···

on 6/13/03 6:32 AM, Robert Klemme at bob.news@gmx.net wrote: