如何检查一个字符串是否在两个字符串之间,并用regex返回以下字符

2024-09-30 22:24:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检查一个字符串是否在一个文本文件中,在另外两个字符串之间,如果为真,则返回与正则表达式匹配的下一个字符串。。。你知道吗

我不知道该怎么做!你知道吗

既然你可能听不懂我的解释,我就用我的问题来解释:

我正在创建一个应用程序(在python中)读取pdf并将其转换为.txt。你知道吗

在这个txt文件中,我想找到pH并返回它。我知道我会在第10节和第11节之间找到它,就像这样:

10. blablablablabla pH 7,6 blablablabla 11.

所以呢

  1. 我怎样才能在"10.""11."之间减少我的研究?你知道吗
  2. 对于pH部分,我认为这是类似于:

    if 'pH' in open(file).read():

  3. 如果我们找到'ph',我如何编码希望下一个字符串遵循这个正则表达式:re.search("[0-9]{1}[,.]?[0-9]?", file)


Tags: 文件字符串intxt应用程序readifpdf
3条回答

我将使用以下方法:

regex = re.compile(r"\b10\.(?:(?!\b11\.|\bpH\b).)*\bpH\b\s*(\d+(?:[.,]\d+)?)(?=.*\b11\.)", re.DOTALL)
pH = regex.search(my_string).group(1)

测试它live on regex101.com。你知道吗

它只匹配pH值,如果它在10.11.之间,如果有多个pH值,它会找到第一个pH值。你知道吗

说明:

\b10\.        # Match 10. (but not 110.)
(?:           # Start of a (repeating) group that matches...
 (?!          #   (if we're not at the start of either...
  \b11\.      #    the number 11.
 |            #    or  
  \bpH\b      #    the string pH
 )            #   )
 .            # any character (including newlines, therefore the DOTALL option).
)*            # Repeat as necessary.
\bpH\b        # Match the string pH
\s*           # Match optional whitespace
(             # Match and capture in group 1:
 \d+          # At least one digit
 (?:[.,]\d+)? # optionally followed by a decimal part
)             # End of capturing group 
(?=           # Assert that the following can be matched afterwards:
 .*           # any number of characters
 \b11\.       # followed by 11.
)             # End of lookahead assertion.

如果你能在234点放任何你想要的东西,这应该是可行的。它返回匹配“234”的pH符号之后的所有内容。你知道吗

import re

my_str = "10. blablablablabla pH 1234 11. 234"
match_list = re.findall(r'10\..*pH.*(234).*11\.', my_str)

print(match_list)

抽象地,这将查找与以下模式匹配的字符串:start_pattern wildcard pre_pattern wildcard captured_pattern wildcard end_pattern所有通配符都是.*,它匹配任何字符的0次或多次出现。捕获的模式位于两个大括号(my_pattern)之间,在本例中是234

为了更好地说明我的最后一点,下面是上面的变量:

import re

start_pattern = "10\."
end_pattern = "11\."
pre_pattern = "pH"
wildcard = '.*'
captured_pattern = "234"

my_str = "10. blablablablabla pH 1234 11. 234"

match_list = re.findall(r''
                        + start_pattern
                        + wildcard
                        + pre_pattern
                        + wildcard
                        + '(' + captured_pattern + ')'
                        + wildcard
                        + end_pattern
                        , my_str)

print(match_list)

如果我理解正确,我假设以10.开头的行总是以11.结尾。如果是这样,我们只需要找到10.,然后检查后面的内容:

10\.\s.+(?<=pH )(\d[.,]?\d)(?=\s)

这与10.匹配,然后匹配任何前面有"pH "的数字(使用后面的正查找)。然后它将捕获限制为2位数字,可以选择用句点或逗号分隔

see demo here

更新

根据注释中的说明,它现在具有11.结束分隔符,并捕获找到的第一个“pH”之后所需的数字

\b10\.\s.+(?<=pH )(\d[.,]?\d)\s.+?\b11\.

updated demo

相关问题 更多 >