如何在regex中的特定模式之间进行匹配

  • 本文关键字:之间 模式 regex python regex
  • 更新时间 :
  • 英文 :


这是文本

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

我想要的输出是

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

也就是说,每个文本都在no1之间。2.,2.3.,3.-4.等等

那么我该如何进行类似匹配文本b/w'[0-9][.]'模式的操作

我想提取数字之间的文本,这样我就可以得到数字之间的文字

这里有一种方法:

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

  • d+.:匹配一到无限次之间的任何数字(贪婪(,后跟一个点和一个空格
  • (.*?):匹配零到无限次数之间的任何字符,尽可能少
  • (?=):积极展望
  • |:或者
  • $:匹配文本的末尾

在python中:

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

使用CCD_ 6使得CCD_。

您可以根据段落编号和文本的re.split进行匹配。^d+.匹配一行开头的数字,后面跟着一个句点和一个空格;在上下文中,它可能看起来像这样:

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. Yournn2. Longnn3. 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)

它会给你一个字符串列表,比如:

['Yournn', 'Longnn', 'Text']

最新更新