簡體   English   中英

如何匹配正則表達式中特定模式之間的內容

[英]how to match something in between a particular pattern in 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

我想要的是 output

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

那就是每個文本都在 no 1. 2.,2. 之間。 3.,3.-4。 等等

那么我怎樣才能做一些類似匹配文本黑白'[0-9][.]'模式的事情

我想提取數字之間的文本,這樣我就可以得到數字之間的文本

這是一種方法:

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

  • \d+\. : 匹配一次和無限次之間的任何數字(貪婪),后跟一個點和一個空格。
  • (.*?) :匹配零次到無限次之間的任何字符,盡可能少。
  • (?=) :正面前瞻。
  • | : 或者。
  • $ :匹配文本的結尾。

在 python 中:

import re

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

re.S用於使. 匹配一個新行。

您可以匹配段落編號並按它們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. 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)

這會給你一個字符串列表,比如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM