Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Complex GSUB query

Reply
Thread Tools

Complex GSUB query

 
 
Ne Scripter
Guest
Posts: n/a
 
      10-20-2009
Hello all,

I am struggling with something and I have yet been able to find anything
that may help me.

I have a string like follows:

string = "\"bloggs, Joe (JBloggs, INFO)\" joebloggs, \"bloggs, Bill
(BBloggs, INFO)\" billbloggs"

I want to break this string up into two entries using a _ seperator, one
for Joe and the other for Bill. I could do this with a simple

string.gsub(",", "_")

However the problem with doing this is that there are commas elsewhere
in the string. So what I need to say is, if the comma is outside of ""
(quotes) replace it with the _

Could anyone possibly help me with this?

Thanks
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Aldric Giacomoni
Guest
Posts: n/a
 
      10-20-2009
Ne Scripter wrote:
>
> string = "\"bloggs, Joe (JBloggs, INFO)\" joebloggs, \"bloggs, Bill
> (BBloggs, INFO)\" billbloggs"
>
> I want to break this string up into two entries using a _ seperator, one
> for Joe and the other for Bill. I could do this with a simple
>
> string.gsub(",", "_")
>
> However the problem with doing this is that there are commas elsewhere
> in the string. So what I need to say is, if the comma is outside of ""
> (quotes) replace it with the _
>
> Could anyone possibly help me with this?
>
> Thanks


I'd do:
string.gsub!(", \"", "_ \"")
# If the comma is followed by a space and double quotes, replace that
with an undersore, a space and a double quote.
But that's because I'm really lazy.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      10-20-2009
Hi --

On Wed, 21 Oct 2009, Ne Scripter wrote:

> Hello all,
>
> I am struggling with something and I have yet been able to find anything
> that may help me.
>
> I have a string like follows:
>
> string = "\"bloggs, Joe (JBloggs, INFO)\" joebloggs, \"bloggs, Bill
> (BBloggs, INFO)\" billbloggs"
>
> I want to break this string up into two entries using a _ seperator, one
> for Joe and the other for Bill. I could do this with a simple
>
> string.gsub(",", "_")
>
> However the problem with doing this is that there are commas elsewhere
> in the string. So what I need to say is, if the comma is outside of ""
> (quotes) replace it with the _
>
> Could anyone possibly help me with this?


It looks like the pattern /, "/ occurs at the end of one record into
the beginning of the next one, and nowhere else. Assuming that's
correct, it suggests something like:

string.gsub(/,(?=\s+")/, '_')

i.e., for any comma which is followed by some whitespace and a double
quote character, replace the comma with an underscore.


David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      10-20-2009

On Oct 20, 2009, at 11:37 AM, David A. Black wrote:

> Hi --
>
> On Wed, 21 Oct 2009, Ne Scripter wrote:
>
>> Hello all,
>>
>> I am struggling with something and I have yet been able to find
>> anything
>> that may help me.
>>
>> I have a string like follows:
>>
>> string = "\"bloggs, Joe (JBloggs, INFO)\" joebloggs, \"bloggs, Bill
>> (BBloggs, INFO)\" billbloggs"
>>
>> I want to break this string up into two entries using a _
>> seperator, one
>> for Joe and the other for Bill. I could do this with a simple
>>
>> string.gsub(",", "_")
>>
>> However the problem with doing this is that there are commas
>> elsewhere
>> in the string. So what I need to say is, if the comma is outside of
>> ""
>> (quotes) replace it with the _
>>
>> Could anyone possibly help me with this?

>
> It looks like the pattern /, "/ occurs at the end of one record into
> the beginning of the next one, and nowhere else. Assuming that's
> correct, it suggests something like:
>
> string.gsub(/,(?=\s+")/, '_')
>
> i.e., for any comma which is followed by some whitespace and a double
> quote character, replace the comma with an underscore.
>
>
> David
>
> --
> The Ruby training with D. Black, G. Brown, J.McAnally
> Compleat Jan 22-23, 2010, Tampa, FL
> Rubyist http://www.thecompleatrubyist.com
>
> David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)


Or perhaps scan is a better hammer for this nail:

irb> string = "\"bloggs, Joe (JBloggs, INFO)\" joebloggs, \"bloggs,
Bill (BBloggs, INFO)\" billbloggs"
=> "\"bloggs, Joe (JBloggs, INFO)\" joebloggs, \"bloggs, Bill
(BBloggs, INFO)\" billbloggs"
irb> re = %r{"\w+, \w+ \(\w+, \w+\)" \w+}
=> /"\w+, \w+ \(\w+, \w+\)" \w+/
irb> string.scan(re)
=> ["\"bloggs, Joe (JBloggs, INFO)\" joebloggs", "\"bloggs, Bill
(BBloggs, INFO)\" billbloggs"]

You could paste them back together with a .join('_'), but I suspect
that you want the pieces later anyway.

-Rob

Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)




 
Reply With Quote
 
Ne Scripter
Guest
Posts: n/a
 
      10-21-2009
Thanks all. I went the suggestion given by David because although the
structure is consistent I can never be sure on the number of elements in
the string.

Many thanks


Rob Biedenharn wrote:
> On Oct 20, 2009, at 11:37 AM, David A. Black wrote:
>
>>> I have a string like follows:
>>> However the problem with doing this is that there are commas

>>
>> Compleat Jan 22-23, 2010, Tampa, FL
>> Rubyist http://www.thecompleatrubyist.com
>>
>> David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

>
> Or perhaps scan is a better hammer for this nail:
>
> irb> string = "\"bloggs, Joe (JBloggs, INFO)\" joebloggs, \"bloggs,
> Bill (BBloggs, INFO)\" billbloggs"
> => "\"bloggs, Joe (JBloggs, INFO)\" joebloggs, \"bloggs, Bill
> (BBloggs, INFO)\" billbloggs"
> irb> re = %r{"\w+, \w+ \(\w+, \w+\)" \w+}
> => /"\w+, \w+ \(\w+, \w+\)" \w+/
> irb> string.scan(re)
> => ["\"bloggs, Joe (JBloggs, INFO)\" joebloggs", "\"bloggs, Bill
> (BBloggs, INFO)\" billbloggs"]
>
> You could paste them back together with a .join('_'), but I suspect
> that you want the pieces later anyway.
>
> -Rob
>
> Rob Biedenharn http://agileconsultingllc.com
> (E-Mail Removed)


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How complex is complex? Kottiyath Python 22 03-28-2009 10:11 PM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
gsub and gsub! are inconsistent aurelianito Ruby 9 11-09-2005 01:38 PM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM



Advertisments