简体   繁体   English

RE:a的数量可以被6整除,b的数量可以被8整除

[英]RE: Number of a's is divisible by 6 and Number of b's is divisible by 8

Find a regular expression which represents strings made of { a, b }, where number of a 's is divisible by 6 and number of b 's is divisible by 8 . 找到一个表示由{ a, b }组成的字符串的正则表达式,其中a的数字可以被6整除, b的数量可以被8整除。

I tried to create a DFA which accepts such strings. 我试图创建一个接受这种字符串的DFA。 My idea was to use all the remainders mod 6 and mod 8 leading to a total of 48 remainders. 我的想法是使用所有余数mod 6和mod 8导致总共48个余数。 Thus each state in DFA is a pair (r, s) where r varies from 0 to 6 and s varies from 0 to 7. Start state (as well as accepting state) is (0, 0) and by we can easily give transitions to states by noting that if we input "a" the state (r, s) transitions to (r + 1, s) and on input "b" it transitions to state (r, s + 1) . 因此,DFA中的每个状态都是一对(r, s) ,其中r从0到6变化, s从0到7变化。开始状态(以及接受状态)是(0, 0) ,我们可以很容易地给出转换通过注意到如果我们输入"a" ,状态(r, s)转换为(r + 1, s)并且输入"b"它转换到状态(r, s + 1)

However it is too difficult to work with a DFA of 48 states and I am not sure if this can be minimized by using the DFA minimization algorithm by hand. 然而,使用48个状态的DFA太难了,我不确定是否可以通过手动使用DFA最小化算法来最小化这种情况。

I am really not sure how then we can get to a regular expression representing such strings. 我真的不确定我们怎么能得到一个表示这种字符串的正则表达式。

If you are allowed to use lookaheads: 如果允许使用前瞻:

^(?=b*((ab*){6})+$)a*((ba*){8})+$

正则表达式可视化

Debuggex Demo Debuggex演示

Example of matched string: bbaabbaabbaabb 匹配字符串的示例: bbaabbaabbaabb

Idea is simple: We know how to match string having number of a s divisible by 6 - ^((b*ab*){6})+$ , also we know how to match string having number of b s divisible by 8 - ^((a*ba*){8})+$ . 想法很简单:我们知道如何搭配串具有数a由6 S整除- ^((b*ab*){6})+$ ,我们也知道如何搭配具有数字串b小号被8整除- ^((a*ba*){8})+$ So we just put one regex to lookahead, and another one to matching part. 所以我们只是将一个正则表达式用于预测,另一个用于匹配部分。

In case if you also need to match strings consisting only of a s or only of b s, then the following regex will do: 如果您需要匹配仅a s或仅包含b s的字符串,则以下正则表达式将执行以下操作:

^(?=b*((ab*){6})*$)a*((ba*){8})*$

正则表达式可视化

Examples of matched strings: aaaaaa , bbbbbbbb , bbaabbaabbaabb 匹配字符串的示例: aaaaaabbbbbbbbbbaabbaabbaabb

Debuggex Demo Debuggex演示

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

相关问题 正则表达式的二进制数可被8或奇数除 - Regex for binary number divisible by 8 OR odd 使用C#.Net可以被60整除的任何数字的正则表达式? - Regular Expression for any number divisible by 60 using C# .Net? 设计DFA接受可被数字'n'整除的二进制字符串 - Design DFA accepting binary strings divisible by a number 'n' 查找包含偶数个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? 带有偶数个 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 正则表达式“以 a 开头并以 b 结尾的 a 和 b 数量相等的字符串”。 - Regex for "strings of equal number of a's and b's that beginns with an a and ending with b."r 字母表 {a, b} 中的正则表达式,用于查找 a 的数量大于 b 的数量的字符串 - Regular expression from the alphabet {a, b} that finds strings wherer the numbers of a's is bigger than the number of b's 包含a和偶数b的字符串的正则表达式 - Regular expression for strings that cointains a and an even number of b's 正则表达式过滤器数可被3除 - Regex filter numbers divisible by 3 正则表达式 - 可被 500 整除 - Regular Expression - Divisible by 500
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM