[newbie] regexp - match quotation mark

Hi, I’m trying to write a regular expression to match a quotation mark
inside a string. Actually, what I want is to extract the text between two
quotation marks, like this:

This should be “simple”, but I can’t do it.

I want to extract the word simple with a regular expression.

Thanks a lot!

···


“If it was so, it might be; and if it were so, it would be; but as it isn’t,
it ain’t. That’s logic” – Lewis Carroll

Hi, I’m trying to write a regular expression to match a quotation mark
inside a string. Actually, what I want is to extract the text between
two quotation marks, like this:

This should be “simple”, but I can’t do it.

I want to extract the word simple with a regular expression.

if /“([^”]*)/ =~ ‘This should be “simple”, but I can't do it.’
puts $1
end

I would recommend Mastering Regular Expressions by Jeff Friedl, from
Oreily.

Hope that helps,

Walt

···

Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284


Hi, I’m trying to write a regular expression to match a quotation mark
inside a string. Actually, what I want is to extract the text between two
quotation marks, like this:

This should be “simple”, but I can’t do it.

I want to extract the word simple with a regular expression.

Thanks a lot!

should fit:

rgx=/“([^”]+)/
=> /“([^”]+)/
m= rgx.match ‘this is my “simple” sting’
=> #MatchData:0x27c5760
m[1]
=> “simple”

read like:
match a " and then read any charcater while it is not a "

not that it does not handle escaped "

···

il Fri, 26 Mar 2004 13:15:13 +0100, “Einar Buffer” _ebuffer_@hotmail.com ha scritto::

“gabriele renzi” surrender_it@remove.yahoo.it schrieb im Newsbeitrag
news:lfb860lpiprtl3drl90n19ron1303i9n2p@4ax.com

Hi, I’m trying to write a regular expression to match a quotation mark
inside a string. Actually, what I want is to extract the text between
two
quotation marks, like this:

This should be “simple”, but I can’t do it.

I want to extract the word simple with a regular expression.

Thanks a lot!

should fit:

rgx=/“([^”]+)/
=> /“([^”]+)/
m= rgx.match ‘this is my “simple” sting’
=> #MatchData:0x27c5760
m[1]
=> “simple”

read like:
match a " and then read any charcater while it is not a "

not that it does not handle escaped "

This might help.

robert

module Scanner

RX=/
“((?:[^”\]|\.))" | # double quoted strings
‘((?:[^’\]|\.)
)’ | # single quoted strings
(\S+) # others
/ox

def self.scan_non_ws(str, rx=RX)
index = 0
tokens = block_given? ? nil :

str.scan( rx ) do |m|
token = m.detect{|x|not x.nil?}
# puts “m=#{m.inspect}”
# token = ( m[0]||m[1]||m[2] )
# p token

if tokens
  tokens << token
else
  yield token, index
  index += 1
end

end

tokens
end

end # module Scanner

···

il Fri, 26 Mar 2004 13:15:13 +0100, “Einar Buffer” > _ebuffer_@hotmail.com ha scritto::

“Robert Klemme” bob.news@gmx.net wrote in message
news:c41b05$2dp8jh$1@ID-52924.news.uni-berlin.de

“gabriele renzi” surrender_it@remove.yahoo.it schrieb im Newsbeitrag
news:lfb860lpiprtl3drl90n19ron1303i9n2p@4ax.com

Hi, I’m trying to write a regular expression to match a quotation mark
inside a string. Actually, what I want is to extract the text between
two
quotation marks, like this:

This should be “simple”, but I can’t do it.

I want to extract the word simple with a regular expression.

Thanks a lot!

should fit:

rgx=/“([^”]+)/
=> /“([^”]+)/
m= rgx.match ‘this is my “simple” sting’
=> #MatchData:0x27c5760
m[1]
=> “simple”

read like:
match a " and then read any charcater while it is not a "

not that it does not handle escaped "

This might help.

robert

module Scanner

RX=/
“((?:[^”\]|\.))" | # double quoted strings
‘((?:[^’\]|\.)
)’ | # single quoted strings
(\S+) # others
/ox

def self.scan_non_ws(str, rx=RX)
index = 0
tokens = block_given? ? nil :

str.scan( rx ) do |m|
token = m.detect{|x|not x.nil?}
# puts “m=#{m.inspect}”
# token = ( m[0]||m[1]||m[2] )
# p token

if tokens
  tokens << token
else
  yield token, index
  index += 1
end

end

tokens
end

end # module Scanner

Hi, thank you very much for your informative replies! In addition to being
very new to ruby and not really experienced with regular expressions in
general, it turns out I was also confused by the editor I’ve been using for
my first few ruby scripts - SciTE seems to be confused by regular
expressions, and so led me to believe that something was wrong with my
regular expression while in reality it was perfectly legal…

Many regards,
Einar

···

il Fri, 26 Mar 2004 13:15:13 +0100, “Einar Buffer” > > _ebuffer_@hotmail.com ha scritto::