Ask a Question related to Ruby, Design and Development.
-
Rob Partington #1
regexp indexes to strings
I like this, but can it be extended to work for replacing?
irb(main):001:0> a="123-456-1239"
=> "123-456-1239"
irb(main):002:0> a[/123/]
=> "123"
irb(main):003:0> a[/123/]="xxx"
=> "xxx"
irb(main):004:0> a
=> "xxx-456-1239"
This is good stuff. Except it doesn't extend.
irb(main):005:0> a="123-456-1239"
=> "123-456-1239"
irb(main):006:0> a[/123/, 2]="xxx"
IndexError: index 2 out of regexp
from (irb):6:in `[]='
from (irb):6
irb(main):007:0> a[/123/, 1]="xxx"
IndexError: index 1 out of regexp
from (irb):7:in `[]='
from (irb):7
irb(main):008:0> a
=> "123-456-1239"
Bah! This would really rule.
Rob Partington Guest
-
indexes in cs
Is it possible to easily create an InDesign index from a Word document? In other words, if the Word doc comes through with a special character... -
using indexes
quick question regarding proper use of indexes. there are options for asc/desc sorting for each column when creating the index - for optimal... -
Extracting strings delimited by other strings
Hi, I need to write some code that will allowed embedded, specially formatted comments to document test cases within a program (SAS code). The... -
Indexes
hello, here is my question. if you have a table called related_to and it has these fields: id active from_class_id from_id type_cid... -
Two indexes?
I'm doing a book with a long contributor index as well as a standard index. Is it possible to use ID's indexing feature for both, or am I limited to... -
ts #2
Re: regexp indexes to strings
>>>>> "R" == Rob Partington <rjp@frottage.org> writes:
R> irb(main):006:0> a[/123/, 2]="xxx"
R> IndexError: index 2 out of regexp
Well, this case is for
svg% ruby -e 'a="123-456-1239"; a[/1(2)3/, 1] = "xxx"; p a'
"1xxx3-456-1239"
svg%
svg% ruby -e 'a="123-456-1239"; a[/1(2)(3)/, 2] = "xxx"; p a'
"12xxx-456-1239"
svg%
you can write
svg% ruby -e 'a="123-456-1239"; a[/123/] = "xxx"; p a'
"xxx-456-1239"
svg%
--
Guy Decoux
ts Guest
-
Yan-Fa Li #3
Re: regexp indexes to strings
I think it doesn't work because your regexp only has one capture group.
If you
define multiple capture groups it works just fine:
a="123-456-1239"
a[/([0-9]{3,3})-([0-9]{3,3})-([0-9]{3,4})/,2]="XXX"
I tested this in IRB and it seems work just fine. I don't know if
there's a more efficient way to do
this that is less complex looking but it does work. Let me know if you
need help decoding this regexp,
but I think it's fairly self explanatory.
Yan
Rob Partington wrote:
>This is good stuff. Except it doesn't extend.
>
> irb(main):005:0> a="123-456-1239"
> => "123-456-1239"
> irb(main):006:0> a[/123/, 2]="xxx"
> IndexError: index 2 out of regexp
> from (irb):6:in `[]='
> from (irb):6
> irb(main):007:0> a[/123/, 1]="xxx"
> IndexError: index 1 out of regexp
> from (irb):7:in `[]='
> from (irb):7
> irb(main):008:0> a
> => "123-456-1239"
>
>Bah! This would really rule.
>
>
Yan-Fa Li Guest



Reply With Quote

