简体   繁体   中英

how to match something in between a particular pattern in regex

Here's the Text

1. A resistance of 2 Q is connected across one gap of a metre-bridge 
(the length of the

wire is 100 cm) and an unknown resistance, greater than 2 Q, is connected across
the other gap. When these resistances are interchanged, the balance point shifts by
20 cm. Neglecting any corrections,
 the unknown resistance is

2. In an experiment to determine the focal length (f) of a concave mirror by the
u-v method, a student places the object pin A on the principal axis at a distance x
from the pole P. The student looks at the pin and its inverted image from a distance
keeping his/her eye in line with PA. When the student shifts his/her eye towards left,
the image appears to the right of the object pin. Then,


3. Two particles of mass m each are tied at the ends of
a light string of length 2a. The whole system is kept
on a frictionless horizontal surface with the string
held tight so that each mass is at a distance  from
the center P (as shown in the figure). Now, the
mid-point of the string is pulled vertically upwards with
a small but constant force F. As a result, the particles
move towards each other on the surface. The magnitude
of acceleration, when the separation between them
becomes 2x, is

what I want as output is

A resistance of 2 Q is connected across one gap of a metre-bridge

 (the length of the
wire is 100 cm) and an unknown resistance, greater than 2 Q, is connected across
the other gap. When these resistances are interchanged, the balance point shifts by
20 cm. Neglecting any corrections,
 the unknown resistance is

,

In an experiment to determine the focal length (f) of a concave mirror by the
u-v method, a student places the object pin A on the principal axis at a distance x
from the pole P. The student looks at the pin and its inverted image from a distance
keeping his/her eye in line with PA. When the student shifts his/her eye towards left,
the image appears to the right of the object pin. Then,

,

Two particles of mass m each are tied at the ends of
a light string of length 2a. The whole system is kept
on a frictionless horizontal surface with the string
held tight so that each mass is at a distance  from
the center P (as shown in the figure). Now, the
mid-point of the string is pulled vertically upwards with
a small but constant force F. As a result, the particles
move towards each other on the surface. The magnitude
of acceleration, when the separation between them
becomes 2x, is

that is every text is between no 1. 2.,2. 3.,3.-4. and so on

So how can I do something like match text b/w ' [0-9][.] ' pattern

I want to extract the text between the number so I can get the text between the numbers

Here is one way to do so:

\d+\. (.+?)(?=\d+\. |$)

  • \d+\. : Matches any number between one and unlimited times (greedy), followed by a dot and a space.
  • (.*?) : Matches any character between zero and unlimited times, as few as possible.
  • (?=) : Positive lookahead.
  • | : Or.
  • $ : Matches the end of the text.

In python:

import re

output = re.findall(r"\d+\. (.+?)(?=\d+\. |$)", data, flags=re.S)
print(output)

re.S is used so that the . matches a new line.

You could match the paragraph numbers and re.split your text by them. ^\d+\. matches a number at the beginning of a line, followed by a period and a space; in context it could look like this:

import re

# Multiline is important since we want to match numbers at 
# the beginning of a line only.
expr = re.compile(r"^\d+\. ", re.MULTILINE)

text = """1. Your\n\n2. Long\n\n3. Text"""

# Using a list comprehension to filter empty results;
# "if p" resolves to true if p is not empty.
parts = [p for p in re.split(expr, text) if p]

print(parts)

which would give you a list of strings like:

['Your\n\n', 'Long\n\n', 'Text']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM