1

次の 2 つの属性を持つ 'a' と 'b' を含む文字列の正規表現を検索しています。

4

3 に答える 3

1

どうですか:

/(?=^(?:..)+$)(?!aa)(?=.*a)(?=.*b)/

説明:

/         : delimiter
          : control there are an even number of char
  (?=     : positive lookahead
    ^     : begining of string
    (?:   : non capture group
      ..  : 2 characters
    )+    : one or more times
    $     : end of string
  )
          : control there aren't aa
  (?!     : negative look ahead
    aa    : aa
  )
          : control there is at least an a
  (?=     : positive lookahead
    .*a   : at least an a
  )
          : control there is at least a b
  (?=     : positive lookahead
    .*b   : at least a b
  )
/         : delimiter
于 2012-04-18T10:08:22.943 に答える
1

標準の(古い)正規表現で可能です:

(ab|bb|(ba)*bb)*(ba)*

于 2012-04-18T09:51:09.630 に答える
0

It can be easily done with Perl-compatible regular expressions: ^(ab|bb|(ba(?!a)))*$

Basically it says that string must consist of ab, bb, ba substrings mixed in any order, BUT ba cannot follow another a character.

The string will have even length because all these subexpressions have even length. aa can't appear in a string, because the only way for it to appear is in substring baab, but the regex specifically restricts ba to be followed by a.

于 2012-04-18T09:30:17.917 に答える