如何将不可约分数与正则表达式匹配?
例如,23/25、3/4、5/2、100/101等。
首先,我对正则表达式中的gcd算法实现一无所知。
更新对于所有回答“你使用了错误的工具”的人:
是的,伙计们,我意识到正则表达式通常用于什么。没关系。但这个问题很奇怪,这就是它的全部意义所在。
更新2:这个想法是找到一个在以下情况下可能有用的正则表达式:
$> echo "1/2" | grep -P regex
1/2
$> echo "2/4" | grep -P regex
因此,正则表达式应该只是一个字符串,而不使用任何脚本和变量。只有正则表达式。
实际上,我已经知道一些匹配一元数字系统中编写的可约分数的正则表达式。
$> echo "11/1111" | grep -P '^1/1+$|(11+)+\1+/\1+$'
11/1111
所以问题是在正则表达式中从十进制转换为一元数字系统,但我不知道如何。
How can I match irreducible fractions with regex?
For example, 23/25, 3/4, 5/2, 100/101, etc.
First of all, I have no idea about the gcd-algorithm realization in regex.
Update for all of you who's answering like "You are using the wrong tool":
Yeah, guys, I'm realizing what regex is normally used for. It's okay. But that this question is weird is kind of its whole point.
Updated 2: The idea is to find a regex that could be helpful in a situation like:
gt; echo "1/2" | grep -P regex
1/2
gt; echo "2/4" | grep -P regex
So, the regex should be only a string, without using any scripts and variables. Only regex.
Actually, I already know some regex which match reducible fractions written in the unary number system.
gt; echo "11/1111" | grep -P '^1/1+$|(11+)+\1+/\1+
So the thing is to convert from decimal to unary number system in regex, but I don't know how.
11/1111
So the thing is to convert from decimal to unary number system in regex, but I don't know how.
发布评论
评论(4)
更新
由于海报要求一个与“36/270”等字符串匹配的正则表达式,但表示它的可读性并不重要,因此该正则表达式是:
但是,如果像我一样,您认为难以辨认的正则表达式是绝对不可接受的,您可以将其写得更清晰:
您可以通过将与一元版本匹配的版本与与十进制版本匹配的版本分开来提高可维护性,如下所示:
将其分成两个命名的正则表达式不是更容易吗?现在,这将使 $reducible_rx 与 $decimal_rx 相同,但一元版本是它自己的东西。我就是这样做的,但是原始海报想要一个正则表达式,因此您必须插入嵌套的正则表达式,正如我在上面首先介绍的那样。
无论哪种方式,您都可以使用以下方法插入下面的测试工具:
您将看到它是一个通过所有测试的正确正则表达式,而且使用单个正则表达式来实现这一点,因此现在已经通过了原始问题的所有要求,我声明Qᴜᴏᴅ ᴇʀᴀᴛ ᴅᴇᴍᴏɴsᴛʀᴀɴᴅᴜᴍ:“退出,够了。”
UPDATE
Since the poster requested a single regex that matches against strings like "36/270", but says it doesn’t matter how legible it is, that regex is:
But, if like me, you believe that an illegible regex is absolutely unacceptable, you will write that more legibly as:
You can improve maintainability by splitting out the version that matches the unary version from the one that matches the decimal version like this:
Isn’t that much easier by separating it into two named regexes? That would now make
$reducible_rx
the same as$decimal_rx
, but the unary version is its own thing. That’s how I would do it, but the original poster wanted a single regex, so you’d have to interpolate the nested one for that as I first present above.Either way, you can plug into the test harness below using:
And you will see that it is a correct regex that passes all tests, and does so moreover using a single regex, wherefore having now passed all requirements of the original question, I declare Qᴜᴏᴅ ᴇʀᴀᴛ ᴅᴇᴍᴏɴsᴛʀᴀɴᴅᴜᴍ: “Quit, enough done.” ????
And you’re welcome.
The answer is to match the regex
^(?|1+/(1)|(11+)\1*/\1+)$
against the fraction once it has been converted from decimal to unary notation, at which point the greatest common factor will be found in$1
on a match; otherwise they are coprimes. If you are using Perl 5.14 or better, you can even do this in one step:Which will correctly report that:
(And of course, reducing by 1 means there is no longer a denominator.)
If you wanted to have a bit of punning fun with your readers, you could even do it this way:
Here is the code that demonstrates how to do this. Furthermore, it constructs a test suite that tests its algorithm using all (positive) numerators and denominators up to its argument, or 30 by default. To run it under a test harness, put it in a file named coprimes and do this:
Here is an example of its output when run without the test harness:
And here is the program:
不,这是不可能的。就像一个优秀的计算机科学家一样,我会忽略工具正则表达式的细节,并假设您在询问是否存在正则表达式。我对正则表达式的功能没有足够的了解,无法确保它仅限于正则表达式。抛开这个警告不谈,继续表演吧。
改写一下我们得到:
假设这样的语言是正规的。那么存在一个可以决定
L
中的成员资格的DFA。令 N 为此类 DFA 的状态数。素数有无数个。由于素数的数量是无限的,因此存在任意多个素数大于基数r
中N
位可编码的最大数。 (注意:最大的数字显然是r
的N
次方。我使用这种奇怪的措辞来展示如何容纳一元。)选择N+ 1
大于该数字的素数。所有这些数字都使用至少N+1
位数字(以基数r
表示)进行编码。枚举这些素数p₀
到pₙ
。令sᵢ
为读取/
后pᵢ
的状态。根据鸽巢原理,有N
个状态和N+1
sᵢ
状态,因此至少存在一对索引( j,k)
使得sⱼ = sₖ
。因此,从 DFA 的初始状态开始,输入pₖ/
和pⱼ/
会导致相同的状态sⱼ
(或sₖ< /code>) 和
pⱼ
和pₖ
是不同的素数。L
必须接受所有不同素数对p/q
,因为它们是互质的,并拒绝所有被自身除p/p
的素数作为p
与p
不互质。现在该语言接受pⱼ = pₖ
,因此存在从使用字符串pₖ
的sⱼ
到接受状态的状态序列,将此序列称为 <代码>β。令α
为从初始状态开始读取pₖ
的状态序列。从字符串pₖ/pₖ
的初始状态开始的 DFA 状态序列必须与α
后跟相同β。该序列从初始状态开始,进入
sₖ
(通过读取输入pₖ
),并通过读取pₖ
达到接受状态。 DFA 接受pₖ/pₖ
,并且pₖ/pₖ
位于L
中。pₖ
与pₖ
不互质,因此pₖ/pₖ
不在L
中。矛盾。因此语言L
是不规则的,或者不存在正则表达式。Nope it cannot be done. Like a good computer scientist I will ignore the specifics of the tool regex and assume you are asking if there is a regular expression. I do not have enough knowledge about regex's features to ensure it is restricted to regular expressions. That caveat aside, on with the show.
Rewording this we get:
Assume such a language is regular. Then there exists a DFA that can decide membership in
L
. LetN
be the number of states of such a DFA. There are an infinite number of primes. As the number of primes is infinite, there are arbitrarily many primes greater than the largest number encodable inN
digits in the radixr
. (Note: The largest number is clearlyr
raised to the power ofN
. I am using this weird wording to show how to accommodate unary.) SelectN+1
primes that are greater than this number. All of these numbers are encoded using at leastN+1
digits (in the radixr
). Enumerate these primesp₀
topₙ
. Letsᵢ
be the state of thepᵢ
is in immediately after reading the/
. By the pigeon hole principle, there areN
states andN+1
sᵢ
states so there exists at least one pair of indexes(j,k)
such thatsⱼ = sₖ
. So starting from the initial state of the DFA, inputspₖ/
andpⱼ/
lead to the same statesⱼ
(orsₖ
) andpⱼ
andpₖ
are distinct primes.L
must accept all pairs of distinct primesp/q
as they are coprime and reject all primes divided by themselvesp/p
asp
is not coprime top
. Now the language acceptspⱼ = pₖ
so there is a sequence of states fromsⱼ
using the stringpₖ
to an accepting state, call this sequenceβ
. Letα
be the sequence of states readingpₖ
starting from the initial state. The sequence of states for the DFA starting at the initial state for the stringpₖ/pₖ
must be the same asα
followed byβ
. This sequence starts in an initial state, goes tosₖ
(by reading the inputpₖ
), and reaches an accepting state by readingpₖ
. The DFA acceptspₖ/pₖ
andpₖ/pₖ
is inL
.pₖ
is not coprime topₖ
, and thereforepₖ/pₖ
is not inL
. Contradiction. Therefore the languageL
is irregular, or no regular expression exists.如果您以一元形式编写数字,并使用“:”作为除号,我认为这与可约分数匹配:
然后您可以使用 !~ 查找不匹配的字符串。
基于此:http://montreal.pm.org/tech/neil_kandalgaonkar.shtml
If you write the numbers in unary, and use ":" as the division sign, I think this matches reducible fractions:
You can then use !~ to find strings that don't match.
Based on this: http://montreal.pm.org/tech/neil_kandalgaonkar.shtml
你可以知道,以(0,5)结尾的数字可以被(5)整除,或者以(2,4,6,8,0)结尾的数字可以被2整除。
对于3,4,6,7, 8,9 作为除数,我不期望有这种可能性,对于任意除数也不是这样。
我猜你知道决定被 3 整除的方法 - 构建递归交叉和,它必须能被 3 整除,才能使数字可整除。因此,您可以从数字中消除所有 3、6 和 9,以及 0。对于任意数字,您可以按以下方式进行:
如果结果为空,则该数字可以被 3 整除:
类似的方法也适用于 9,其中您有类似的规则。但是任意除数的通用方法是什么?
You can know, that a number, ending in (0,5) is divisible by (5), or ending in (2,4,6,8,0) is divisible by 2.
For 3,4,6,7,8,9 as divisors, I wouldn't expect a possibility, and not for arbitrary divisors too.
I guess you know the method, to decide divisibility by 3 - to build the rekursive crosssum, which has to be divisible by 3, to make the number divisible. So there you could eliminate all 3s, 6s and 9s from the number, as well as the 0. For an arbitrary number, you would proceed this way:
If the result is empty, the number was divisible by 3:
A similar approach could work for 9, where you have a similar rule. But a general approach for arbitrary divisors?