简体   繁体   English

查找包含偶数个a和偶数个b的所有a和b字符串的正则表达式?

[英]Regular expression to find all strings of a's and b's that contain an even number of a's and an even number of b's?

Is it possible to count the number of times that a character has occured in a string using regular expression? 是否可以使用正则表达式计算字符在字符串中出现的次数? Can any regular expression be given to find the all strings of a's and b's that contain an even number of a's and an even number of b's? 是否可以给出任何正则表达式来查找包含偶数个a和偶数个b的a和b的所有字符串?

There is a simple enough finite state machine: it has four states: s00, s01, s10, and s11 depending on whether you have consumed an even or odd number of a s and an even or odd number of b s. 有一个简单的有限状态机:它有四个状态:s00,s01,s10和s11,具体取决于您是否消耗a s的偶数或奇数以及b的偶数或奇数。 The start state (also the end state) is the state reached by consuming an even number of both a s and b s. 起始状态(也就是结束状态)是通过消耗偶数a s和b s达到的状态。 The transition function looks like this: 过渡函数如下所示:

d(s00, a) = s10
d(s00, b) = s01
d(s10, a) = s00
d(s10, b) = s11
d(s01, a) = s11
d(s01, b) = s00
d(s11, a) = s01
d(s11, b) = s10

We can eliminate state s11 : 我们可以消除状态s11

d(s00, a)  = s10
d(s00, b)  = s01
d(s10, a)  = s00
d(s10, ba) = s11
d(s10, bb) = s10
d(s01, b)  = s00
d(s01, aa) = s01
d(s01, ab) = s10

From this we can develop a regular expression without lookahead by tracing all possible paths through the FSM that return once to the start state, and repeating: 由此,我们可以通过跟踪返回到起始状态一次的FSM的所有可能路径来开发正则表达式而无需提前查找,然后重复执行:

( a (bb|ba(aa)*ab)* (a|ba(aa)*b) | b (aa|ab(bb)*ba)* (b|ab(bb)*a) )*

(Meaningless blanks inserted to help me keep track of nesting of parentheses.) The idea is, if the first character is a you reach s10 ; (插入无意义的空格可以帮助我跟踪括号的嵌套。)这个想法是,如果第一个字符是a ,则达到s10 then you can transition to s10 and back to s01 repeatedly via (bb|ba(aa)*ab)* , and finally return to s00 (without repeating s10 ) either via a or via ba(aa)*b . 那么您可以通过(bb|ba(aa)*ab)*反复转换到s10并返回到s01 ,最后通过a或通过ba(aa)*b返回到s00 (不重复s10 )。 A similar pattern (just swap the occurrences of a and b ) gets you from s00 back to s00 via a string that starts with b . 类似的模式(只需交换出现的ab )就可以通过以b开头的字符串将您从s00返回到s00 And you can make as many trips out and back to s00 as you like starting with either a or b . ab开始,您可以进行任意多次往返s00旅行。

Yes it is possible using this lookahead based regex: 是的,可以使用此基于前瞻的正则表达式:

^(?=(?:b*ab*a)*b*$)(?=(?:a*ba*b)*a*$)[ab]*$

RegEx Demo 正则演示

(?=(?:b*ab*a)*b*$) is a lookahead that makes sure there are even number of a s in the input by matching 0 or more pairs of b*a sub-pattern. (?=(?:b*ab*a)*b*$)是超前的,它通过匹配0 或更多 b*a子模式来确保输入中偶数a s。

Similar check is done for even no of b s in (?=(?:a*ba*b)*a*$) 对于(?=(?:a*ba*b)*a*$)b没有进行类似的检查

暂无
暂无

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

相关问题 包含a和偶数b的字符串的正则表达式 - Regular expression for strings that cointains a and an even number of b's 带有偶数个 b 后跟字母 c 后跟奇数个 a 的字符串的正则表达式 - Regular Expression for Strings with even number of b's followed by the letter c followed by an odd number of a's 偶数个1的位串的正则表达式 - Regular expression for bit strings with even number of 1s 如何为包含“a”,“b”和“c”但不超过2“b”和3“c”的所有字符串编写简洁的正则表达式 - How to write a concise regular expression for all strings containing “a”s, “b”s, and “c”s but no more than 2 “b”s and 3 “c”s 正则表达式匹配偶数为1的字符串,并以0开头和结尾 - Regular Expression to match a string with an even number of 1's and starts and ends with a 0 在perl中编写一个正则表达式,用于a的序列,后跟相等数量的b - to write a regular expression in perl for sequence of a's followed by an equal number of b's 正则表达式将字符串与z的相同数量匹配,而b与y相同 - Regular expression that matches Strings with the same amount of a's as z's and b's as y's 找到一个正则表达式来描述那些在字母表 {a,b} 中不包含两个连续 a 的单词 - Find a regular expression that describes those words that don't contain two consecutive a's over the alphabet {a,b} 一个 FA 接受所有具有偶数个 0 且 1 的数量是 3 的倍数的二进制字符串 - An FA that accepts all binary strings with an even number of 0's and the number of 1's is a multiple of 3 正则表达式中\\ b和\\ s之间的差异 - Difference between \b and \s in Regular Expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM