Regular regex for defining periodic lines

The period of a pline wis any natural number psuch that w[i]=w[i+p]when both sides of this equation are defined. Denote by the per(w)size of the smallest period w. We say that the string wis equal to periodic iff per(w) <= |w|/2.

So unofficially a periodic line is just a line that consists of a prefix repeated at least twice. The only complication is that at the end of the line we do not need a full copy of the prefix.

For example, consider a line x = abcab. per(abcab) = 3like x[1] = x[1+3] = a, x[2]=x[2+3] = band there is no shorter period. Therefore, the row is abcabnot periodic. However, the string ababais periodic as per(ababa) = 2.

As additional examples abcabca, ababababaand abcabcabcare also periodic.

Is there a regular expression to determine if a string is periodic or not?

Actually, I don't mind what the taste of the regular expression tastes like, but if it matters, all that Python supports re.

+4
source share
3 answers

What you need is a backlink

\b(\w*)(\w+\1)\2+\b

This corresponds to the even abcabcaand ababababa.

, backreference ( ) , .

+5

Regex.

(.+)\1+. (), .+. \1 ( ) .

ababa ab 1- .

abcab .

, , : ^(.+)\1+. , , . , , , .

@tobias_k , ^((.+)(?:.*))\1+\2?$. ( , ), , .

, .

https://regex101.com/r/jQ3yY1/2

, , ^((.+?)(?:.*?))\1+\2?$.

+4

, ^(.+)(.*)(\1\2)+\1?$.

  • ^...$
  • (.+) , (, a ababa)
  • (.*) , , (, b ababa)
  • (\1\2)+
  • \1?

Python:

>>> p = r"^(.+)(.*)(\1\2)+\1?$"
>>> re.match(p, "abcab")
None
>>> re.match(p, "abcabca")
<_sre.SRE_Match at 0x7f5fde6e51f8>

, "", . , , . |^$ .

+4

All Articles