Re: simple gsub question \' \` what?



Alle venerdì 30 marzo 2007, Dustin Anderson ha scritto:
Sorry for the simple question, I just can't figure this out after a
couple hours...

I need to parse a string:

string = column-1:block-0,block-2,block-1,block-3

and I need to extract everything before the : (colon) into one variable
(should end up like this:
string1 = column-1

and then the rest of the string (block-0,block-2,block-1,block3) into an
array...

How can I use gsub to extract everything before the colon?

I've looked at the documentation and just can't understand how to use
the \` or \' syntax... I want the two new strings to be:

string1 = column-1
string2 = ["block-0", "block-2", "block-1", "block-3"]

I'm thinking it's something like this:

string.gsub(/:/, "\`")

really, I just don't get it...

the documentation that I'm looking at is here:
http://dev.rubycentral.com/ref/ref_c_string.html

thanks a bunch!
-Dustin


I don't think you need gsub here. gsub is used to replace parts of a string
with other characters. What you need is a regexp. This should work:

string.match( /^([^:]*):(.*)$/ )
string1=$1
string2=$2.split(',')

The regexp will put anything from the beginning of the string until the
first : (excluded) in the global variable $1 and anything after the : in the
variable $2. You can then convert $2 to an array of string using the
String#split method.

I hope this helps

Stefano

.



Relevant Pages

  • Re: How do I pull the info from this PostScript?
    ... Do you just want to extract the title from a file, ... When anchorsearch returns true, the 'post' string ... SourceArrayDecode filter from the array, and cvx exec the filter. ...
    (comp.lang.postscript)
  • Re: Newbie: Array of pointers to strings questions.
    ... > What I'm trying to do is extract information from one file and insert ... The first file has a string of ... > char *ptr; ... line1 is just a constant pointer to a character. ...
    (comp.lang.c)
  • Split Single Address Field into Component Parts
    ... would make a copy of the database before making any ... would subtract that part of the string from the working ... I ran an update query to extract the portion of the ...
    (microsoft.public.access.queries)
  • Split Single Address Field into Component Parts
    ... >address field using an update query. ... >string functions. ... I ran an update query to extract the portion of the ...
    (microsoft.public.access.queries)
  • Re: Help With Regular Expression (Another Post)
    ... How can I extract the State from the path and set that to a variable. ... Elseif s="Florida" Then ... 'if function returns an empty string then it means that no applicable state ... dim vState as Variant ...
    (microsoft.public.vb.general.discussion)