Question on regexp 
Author Message
 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!



Tue, 29 Nov 2005 06:56:21 GMT  
 Question on regexp



Quote:
> 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



Tue, 29 Nov 2005 17:15:18 GMT  
 Question on regexp

Quote:


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

--
Regards,
  JJ

Be Kind, Be Careful, Be Yourself



Tue, 29 Nov 2005 19:12:10 GMT  
 Question on regexp



Quote:

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

  end

  def ===(line)

      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

Quote:
> 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.

Quote:
> Thanks again!

You're welcome.

Regards

    robert



Tue, 29 Nov 2005 20:18:47 GMT  
 Question on regexp


Quote:
> 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



Wed, 30 Nov 2005 01:01:18 GMT  
 Question on regexp
On Fri, 13 Jun 2003 07:56:21 +0900

Quote:

> 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



Tue, 29 Nov 2005 23:57:10 GMT  
 Question on regexp
on 6/13/03 6:18 PM, Jason Creighton at

Quote:

> On Fri, 13 Jun 2003 07:56:21 +0900

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

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

Thanks!

--
Regards,
  JJ

Finally using a Mac!



Wed, 30 Nov 2005 07:48:29 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. a question about regexp

2. A newbie question (about regexp)

3. Regexp question

4. regexp question

5. OT: Regexp question

6. regexp questions

7. regexp expression question

8. regexp question

9. regexp question

10. newbie regexp question

11. regexp question

12. regexp question

 

 
Powered by phpBB® Forum Software