4

Ok the problem is that given a fraction eg:1/3 the answer 0.3333333 should represented as 0.(3) and 0.2325555 as 0.232(5) i have figured out a way to split the string when its a single digit repeating:

using re.findall(r'^(.+?)((.)\3+)$', '42344444' )[0][:-1] (ignoring the 0. before the number)

but i want to know how to do this if the pattern is 0.324324324.. to get 0.(324)

4
  • 1
    Are you given the numbers as a fraction or a floating point? If it's the later, this is impossible. Commented Jul 3, 2012 at 1:11
  • Is the aim to discover recurring patterns of arbitrary length? If so, that cannot be done with regex. Commented Jul 3, 2012 at 1:12
  • actually forget about the fraction part .. say you are give the string 23154545454 how would you split the string into the non recurring part and the recurring part Commented Jul 3, 2012 at 1:13
  • ok to make things simpler lets assume strings are of length 20 Commented Jul 3, 2012 at 1:15

1 Answer 1

2

add + after . in the recurring part:

>>> re.findall(r'^(.+?)((.+)\3+)$', '42344343434' )[0][:-1]
('42344', '343434')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.