Rethinkdb: Regex search and replace

Created on 11 Feb 2015  路  2Comments  路  Source: rethinkdb/rethinkdb

The command could be called substitute (replace would have been nice, but it is already taken).

"foobar".substitute("...", "lum") -> "lumbar"
"foobar".substitute("(.).", lambda m: m['groups'][0]['str'], all=True) -> "foa"

This very common function would help fill in the lack of string manipulation functions.

ReQL_proposal

Most helpful comment

I'd love this. I'm currently doing something like the following for simple find/replace type functions.

Global Find Replace

Search is inside split(' '). Replace is inside.add('+').

r('some long string with spaces').split(' ').reduce(
  function(l, r){
    return l.add('+').add(r)
  }
)

Basic regex search/replace

Search is inside str.match('DO/NOT'). Replace is inside .append('REALLY').

r('some/string/that/i/DO/NOT/like/the/start/of').do(function (str){
  return str.match('DO/NOT').default({end:0}).do(function (res){
    return r.branch(
      res('end').gt(0),
      str.split('', res('end')).do(function(parts){
        return parts.slice(0, res('start'))
          .append('REALLY')
          .union(parts.slice(res('end')))
          .reduce(function(l, r){return l.add(r)})
      }),
      str
    )
  })
})

It would be great to replace that with something shorter.

All 2 comments

I'd love this. I'm currently doing something like the following for simple find/replace type functions.

Global Find Replace

Search is inside split(' '). Replace is inside.add('+').

r('some long string with spaces').split(' ').reduce(
  function(l, r){
    return l.add('+').add(r)
  }
)

Basic regex search/replace

Search is inside str.match('DO/NOT'). Replace is inside .append('REALLY').

r('some/string/that/i/DO/NOT/like/the/start/of').do(function (str){
  return str.match('DO/NOT').default({end:0}).do(function (res){
    return r.branch(
      res('end').gt(0),
      str.split('', res('end')).do(function(parts){
        return parts.slice(0, res('start'))
          .append('REALLY')
          .union(parts.slice(res('end')))
          .reduce(function(l, r){return l.add(r)})
      }),
      str
    )
  })
})

It would be great to replace that with something shorter.

@webmasterkai Those are pretty interesting work-arounds, thanks for sharing.

We'll hopefully get to this soon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sain801028 picture sain801028  路  4Comments

babakness picture babakness  路  4Comments

RaitoBezarius picture RaitoBezarius  路  4Comments

jlhawn picture jlhawn  路  4Comments

encryptio picture encryptio  路  5Comments