1

I am trying to split the data

rest = [" hgod eruehf 10 SECTION 1. DATA: find my book 2.11.111 COLUMN: get me tea","111.2 CONTAIN  i am good"]

match = re.compile(r'(((\d[.])(\d[.]))+\s(\w[A-Z]+:|\w+))')
out = match.search(rest)
print(out.group(0))

I found the pattern as "multiple decimal digit(eg:1. / 1.1. / 1.21.1 etc.,) followed by character till another multiple decimal digit(eg:1. / 1.1. / 1.21.1 etc.,) "

I want to split the data as

  1. DATA: find my book

2.11.111 COLUMN: get me tea

111.2 CONTAIN i am good

Is there any way to split the text data based on the pattern.

0

1 Answer 1

2

You may get the expected matches using

import re
rest = [" hgod eruehf 10 SECTION 1. DATA: find my book 2.11.111 COLUMN: get me tea","111.2 CONTAIN  i am good"]
res = []
for s in rest:
    res.extend(re.findall(r'\d+(?=\.)(?:\.\d+)*.*?(?=\s*\d+(?=\.)(?:\.\d+)*|\Z)', s))

print(res)
# => ['1. DATA: find my book', '2.11.111 COLUMN: get me tea', '111.2 CONTAIN  i am good']

See the Python demo

The regex is applied to each item in the rest list and all matches are saved into res list.

Pattern details

  • \d+ - 1+ digits
  • (?=\.) - there must be a . immediately to the right of the current position
  • (?:\.\d+)* - 0 or more repetitions of a . and then 1+ digits
  • .*? - 0+ chars other than newline, as few as possible
  • (?=\s*\d+(?=\.)(?:\.\d+)*|\Z) - up to the 0+ whitespaces, 1+ digits with a . immediately to the right of the current position, 0 or more repetitions of a . and then 1+ digits, or end of string
Sign up to request clarification or add additional context in comments.

7 Comments

It is not finding "1. DATA:" in string... how to make that
@Cybertron You have no 1. DATA.. in your string in the question. What are the exact requirements for the pattern? What type of context should it match?
Awesome!! working!! Thank you so much how can i find the logic of regex patterns please tell me
@Cybertron Use regex101.com or rick.measham.id.au/paste/explain.pl to see what they match.
that didn't work completely please check the pattern
|

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.