regexp indexes to strings

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139