mdszy
mdszy

Reputation: 3

Match consecutive list of exactly one character in set with regular expressions

I don't think I'll even try to explain this, I don't know the words to, but I'd like to achieve the following:

Given a string like this:

+++>><<<--

I'd like a match to give me: +++, but also match if any of the other characters were in the string consecutively like they are. So if the +++ wasn't there, I'd like to match >>.

I tried using the following regular expression:

([><\-\+]+)

However, given the string above, it would match the entire string, and not the first list of consecutive characters.

If it makes a difference, this is in Ruby (1.9.3).

Upvotes: 0

Views: 628

Answers (1)

beerbajay
beerbajay

Reputation: 20270

Not sure about the ruby bit, but you can do this with backreferences in the pattern:

(.)\1+

What this does is to use a capturing group () to capture any character . followed by any number + of the same character \1. The \1 is a backreference to the the first captured group; in a pattern with more capturing groups \2 would be the second captured group and so on.

Java Example

Pattern p = Pattern.compile("(.)\\1+");
Matcher m = p.matcher("aaabbccaa");
m.find();
System.out.println(m.group(0)); // prints "aaa"

Ruby Example

# Return an array of matched patterns.
string = '+++>><<<--'
string.scan( /((.)\2+)/ ).collect { |match| match.first }

Upvotes: 3

Related Questions