Substring

Hi

How can I get begin- and END-positions (or length) of all matches? (for different substrings)
(is there a trick as with case?)
pos=0; string.each {... } # comparing manually char by char is not nice

thank you
Opti

MatchData, which is returned from Regexp#match,
has all the information.

Regards,
Marcus

···

Am 13.03.2017 um 00:04 schrieb Die Optimisten:

How can I get begin- and END-positions (or length) of all matches? (for
different substrings)
(is there a trick as with case?)

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

You can also use String#scan and access global variables.

irb(main):014:0> "foobar".scan(/./) { p [$`, $&, $'] }
["", "f", "oobar"]
["f", "o", "obar"]
["fo", "o", "bar"]
["foo", "b", "ar"]
["foob", "a", "r"]
["fooba", "r", ""]
=> "foobar"

String lengths can be used for numeric positions.

Cheers

robert

···

On Mon, Mar 13, 2017 at 6:32 AM, <sto.mar@web.de> wrote:

Am 13.03.2017 um 00:04 schrieb Die Optimisten:

How can I get begin- and END-positions (or length) of all matches? (for
different substrings)
(is there a trick as with case?)

MatchData, which is returned from Regexp#match,
has all the information.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Forgot one

irb(main):026:0> "foobar".scan(/./) { p [$`, $&, $', $~] }
["", "f", "oobar", #<MatchData "f">]
["f", "o", "obar", #<MatchData "o">]
["fo", "o", "bar", #<MatchData "o">]
["foo", "b", "ar", #<MatchData "b">]
["foob", "a", "r", #<MatchData "a">]
["fooba", "r", "", #<MatchData "r">]
=> "foobar"

···

On Mon, Mar 13, 2017 at 12:53 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Mon, Mar 13, 2017 at 6:32 AM, <sto.mar@web.de> wrote:

Am 13.03.2017 um 00:04 schrieb Die Optimisten:

How can I get begin- and END-positions (or length) of all matches? (for
different substrings)
(is there a trick as with case?)

MatchData, which is returned from Regexp#match,
has all the information.

You can also use String#scan and access global variables.

irb(main):014:0> "foobar".scan(/./) { p [$`, $&, $'] }
["", "f", "oobar"]
["f", "o", "obar"]
["fo", "o", "bar"]
["foo", "b", "ar"]
["foob", "a", "r"]
["fooba", "r", ""]
=> "foobar"

String lengths can be used for numeric positions.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/