Home > OS >  Regex match pair ocurrences of a specific character
Regex match pair ocurrences of a specific character

Time:04-20

I've been trying to make a regex that satisfies this conditions:

  • The word consists of characters a,b
  • The number of b characters must be pair (consecutive or not)

So for example:

  • abb -> accepted
  • abab -> accepted
  • aaaa -> rejected
  • baaab -> accepted

So far i got this: ([a]*)(((b){2}){1,})

As you can see i know very little about the matter, this checks for pairs but it does still accept words with odd number of b's.

CodePudding user response:

You could use this regex to check for some number of as with an even number of bs:

^(?:a*ba*ba*) $

This looks for 1 or more occurrences of 2 bs surrounded by some number (which may be 0) as.

Demo on regex101

Note this will match bb (or bbbb, bbbbbb etc.). If you don't want to do that, the easiest way is to add a positive lookahead for an a:

^(?=b*a)(?:a*ba*ba*) $

Demo on regex101

CodePudding user response:

Checking an Array of Characters Against Two Conditionals

While you could do this using regular expressions, it would be simpler to solve it by applying some conditional checks against your two rules against an Array of characters created with String#chars. For example, using Ruby 3.1.2:

# Rule 1: string contains only the letters `a` and `b`
# Rule 2: the number of `b` characters in the word is even
#
# @return [Boolean] whether the word matches *both* rules
def word_matches_rules word
  char_array = word.chars
  char_array.uniq.sort == %w[a b] and char_array.count("b").even?
end

words = %w[abb abab aaaa baaab]
words.map { |word| [word, word_matches_rules(word)] }.to_h
#=> {"abb"=>true, "abab"=>true, "aaaa"=>false, "baaab"=>true}

Regular expressions are very useful, but string operations are generally faster and easier to conceptualize. This approach also allows you to add more rules or verify intermediate steps without adding a lot of complexity.

There are probably a number of ways this could be simplified further, such as using a Set or methods like Array#& or Array#-. However, my goal with this answer was to make the code (and the encoded rules you're trying to apply) easier to read, modify, and extend rather than to make the code as minimalist as possible.

  • Related