Re: More Regexp and file load problems




On Jun 27, 9:44 pm, Don Levan <levan...@xxxxxxxxx> wrote:
Hi All,

I am struggling here and would be most appreciative of any help.

I have a regular expression problem I can't seem to figure my way out
of. I had sent a similar request this morning, and gratefully
received two emails from David Black and Axel Etzold. Unfortunately,
I am still having difficulty.

I am trying to extract the font name attribute out of the following
xml (this is an excerpt of a larger file). The weird thing, is that
my regular expression correctly matches the text if I use TextMate's
regular expression 'find in project feature' and with the freeware
Reggy regular expression test tool found on Google code. It also
matches correctly, if I text against a subset of the xml being
parsed. I.E., see this first example.

#!/usr/bin/env ruby
#
# Created by Don Levan on 2007-06-26.
# Copyright (c) 2007. All rights reserved.

string = %q(</TextObj>
</Object>
<ObjectStyle id="0" fontHeight="11" graphicFormat="3843"
fieldBorders="132">
<CharacterStyle mask="16183">
<Font-family codeSet="Roman" fontId="0">Helvetica</Font-family>
<Font-size>9</Font-size>
<Face>256</Face>
<Color>#000000</Color>
</CharacterStyle>")

regexp = Regexp.new(/^\s*<Font-family codeSet=\"\w*\" fontId=\"\d*\">
(\w*)<\/Font-family>\s*$/)

if string =~ regexp
puts "#{$1}"

end

Result: Helvetica

However, if I run this script, there is no result.

#!/usr/bin/env ruby
#
# Created by Don Levan on 2007-06-26.
# Copyright (c) 2007. All rights reserved.

regexp = Regexp.new(/^\s*<Font-family codeSet=\"\w*\" fontId=\"\d*\">
(\w*)<\/Font-family>\s*$/)
file = File.new('/Users/donlevan/Desktop/DDRs/Apple Dealer Price
List.xml')

file.each do |line|
if line =~ regexp
puts "#{$1}"

end
end

I have looked at Hiproct and XML simple, unfortunately I can not get
TextMate configured correctly so I keep getting Loaderrors when I try
to use ruby gems.

Thanks,

Don

You use an xml library, possibly rexml
don't forget require 'rubygems'


.



Relevant Pages

  • Re: extract occurrence of regular expression from elements of XML documents
    ... I have just started to use Python a few weeks ago and until last week I ... Now I would like to use Python in combination with ca. 2000 XML ... documents to search for certain regular expression ... I would then like to record the number of occurrences of the regular ...
    (comp.lang.python)
  • Re: extract occurrence of regular expression from elements of XML documents
    ... Obviously my programming knowledge is pretty basic. ... Now I would like to use Python in combination with ca. 2000 XML documents ... by the available information (e.g. posts using different approaches based on ... of occurrences of the regular expression, and the total number of words. ...
    (comp.lang.python)
  • Re: RegEx
    ... rest of the page will or that it will contain well-formed HTML. ... but i need some assistance on what the regular expression ... > it in as XML and then parse it to get the values. ...
    (microsoft.public.dotnet.general)
  • Re: quick and easy way to parse XML
    ... > are, generally speaking, not powerful enough to parse XML! ... Parsing XML requires a stack (or more precisely, ... some of the available regular expression packages contain ...
    (comp.lang.java.help)
  • Re: Can you please help to make decision?
    ... might be a big barrier for a beginner trying to learn ruby. ... I'm staying out of the Ruby vs. Python debate, ... computer user, ... grasping the concepts and still uses regular expression to this day. ...
    (comp.lang.ruby)