简体   繁体   English

计算字符串中的字符匹配数(仅Regex)?

[英]Count number of character matches in a string (Regex only)?

So I recently had an interview question asking me to determine if a string was balanced or not. 因此,最近我有一个面试问题,要求我确定琴弦是否平衡。 A string is balanced under these two conditions: 字符串在以下两个条件下保持平衡:

  1. The sum of the occurrences of 'a' and 'c' is even. “ a”和“ c”出现的总和是偶数。
  2. The sum of the occurrences of 'b' and 'd' is even. “ b”和“ d”出现的总和是偶数。

So given the example " cccddbba " these conditions hold true. 因此,以示例“ cccddbba ”为例,这些条件成立。 Occurrences of c (3) + occurrences of a (1) is even (1+3=4) and occurrences of d (2) + occurrences of b (2) is even (2+2=4) . c (3)出现+ a (1)even (1+3=4) ,而d (2)出现+ b (2)even (2+2=4) The strings can only contain the characters 'a', 'b', 'c', and 'd'. 字符串只能包含字符“ a”,“ b”,“ c”和“ d”。 The regex should work for any string, not just this example. 正则表达式应该适用于任何字符串,而不仅仅是这个例子。

My task was only to write the regex for this. 我的任务只是为此编写正则表达式。 I assume in the background they count the number of matches of 'a' and 'c' found using my regex and sum them up. 我假设他们在后台对使用我的正则表达式找到的“ a”和“ c”的匹配数进行计数,并将它们相加。 The same goes for 'b' and 'd'. “ b”和“ d”也是如此。

I haven't touched regexes in a while, so I did horrible and only got as far as ^([ac])*?[db]*?([ac])*?$ , which is obviously incorrect. 我已经有一段时间没有接触过正则表达式了,所以我确实很恐怖,只得到了^([ac])*?[db]*?([ac])*?$ ,这显然是不正确的。

Use separate look aheads for each assertion: 对每个断言使用单独的前瞻:

^(?=(([^ac]*[ac]){2})*[^ac]*$)(?=(([^bd]*[bd]){2})*[^bd]*$).*$

See live demo . 观看现场演示

This works basically because ([^ac]*[ac]){2}) matches pairs of [ac] . 这基本上是有效的,因为([^ac]*[ac]){2})匹配成对的[ac] The rest is relatively simple. 其余的相对简单。

Using positive lookahead, you can check whether a and c occur an even number of times, and ditto for b and d: 使用正向前瞻,您可以检查a和c是否出现偶数次,并同理b和d:

^(?=[^ac]*([ac][^ac]*[ac][^ac]*)*$)(?=[^bd]*([bd][^bd]*[bd][^bd]*)*$).*$

Each of the two lookahead groups check that the whole string contains an even number of their respective characters. 两个前瞻组中的每一个均检查整个字符串是否包含偶数个其各自的字符。 Note that each lookahead is anchored at the end ( $ ), to force it to scan until the end of the string. 请注意,每个前瞻都锚定在末尾( $ ),以强制其扫描直到字符串的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM