Sajad Torkamani
Sajad Torkamani

Reputation: 670

How to capture consecutive characters in a string

I have a string 'aabaaa'. How could I use a regex to capture consecutive characters so that I get something like the following?

[['aa'], ['b'], ['aaa']]

I am interested in determining how many times each character occurs consecutively.

Upvotes: 1

Views: 1580

Answers (3)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

Just out of curiosity:

ruby >= 2.4

'aabaaa'.each_char.chunk_while(&:==).map(&:join) # or .map(&:length)
#⇒ ["aa", "b", "aaa"]

ruby >= 2.3 (credits to Cary Swoveland)

'aabaaa'.each_char.chunk(&:itself).map(&:join)

∀ ruby

'aabaaa'.scan(/(\w)(\1*)/).map(&:join)
#⇒ ["aa", "b", "aaa"]

'aabaaa'.scan(/(\w)(\1*)/).map(&:join).map(&:length)
#⇒ [2, 1, 3]

Upvotes: 4

guest271314
guest271314

Reputation: 1

You can use String.prototype.match() with RegExp /(\w)(?=\1|[^\1])\1+|(\w)(?!\2)/g) to match word character followed by one or more of capture group or word not followed by capture group.

Or, as suggested by @mudasobwa use RegExp /(\w)(\1*)/g.

To get .length of each matched group you can create an array, iterate array of matches, push object having property set to either first element of match or the matched group itself. Or utilize Array.prototype.map() to return .length of each matched group.

"aabaaa".match(/(\w)(?=\1)\1+|(\w)(?!\2)/g)

let str = "aabaaa";

let groups = str.match(/(\w)(?=\1)\1+|(\w)(?!\2)/g);

let matches = [];

for (let match of groups) {
  matches.push({[match]: match.length});
}

let len = groups.map(({length}) => length);

console.log(groups, matches, len);

Upvotes: 2

sawa
sawa

Reputation: 168209

In Ruby,

'aabaaa'.scan(/(?<s>(?<c>.)\k<c>*)/).map(&:first)
# => ["aa", "b", "aaa"]

'aabaaa'.scan(/(?<s>(?<c>.)\k<c>*)/).map{|s, _| s.length}
# => [2, 1, 3]

Upvotes: 0

Related Questions