我想检查一个字符串是否在一个文本文件中,在另外两个字符串之间,如果为真,则返回与正则表达式匹配的下一个字符串。。。你知道吗
我不知道该怎么做!你知道吗
既然你可能听不懂我的解释,我就用我的问题来解释:
我正在创建一个应用程序(在python中)读取pdf并将其转换为.txt。你知道吗
在这个txt文件中,我想找到pH
并返回它。我知道我会在第10节和第11节之间找到它,就像这样:
10. blablablablabla pH 7,6 blablablabla 11.
所以呢
"10."
和"11."
之间减少我的研究?你知道吗对于pH部分,我认为这是类似于:
if 'pH' in open(file).read():
如果我们找到'ph',我如何编码希望下一个字符串遵循这个正则表达式:re.search("[0-9]{1}[,.]?[0-9]?", file)
我将使用以下方法:
测试它live on regex101.com。你知道吗
它只匹配pH值,如果它在
10.
和11.
之间,如果有多个pH值,它会找到第一个pH值。你知道吗说明:
如果你能在234点放任何你想要的东西,这应该是可行的。它返回匹配“234”的pH符号之后的所有内容。你知道吗
抽象地,这将查找与以下模式匹配的字符串:
start_pattern wildcard pre_pattern wildcard captured_pattern wildcard end_pattern
所有通配符都是.*
,它匹配任何字符的0次或多次出现。捕获的模式位于两个大括号(my_pattern)
之间,在本例中是234
为了更好地说明我的最后一点,下面是上面的变量:
如果我理解正确,我假设以
10.
开头的行总是以11.
结尾。如果是这样,我们只需要找到10.
,然后检查后面的内容:这与
10.
匹配,然后匹配任何前面有"pH "
的数字(使用后面的正查找)。然后它将捕获限制为2位数字,可以选择用句点或逗号分隔see demo here
更新
根据注释中的说明,它现在具有
11.
结束分隔符,并捕获找到的第一个“pH”之后所需的数字updated demo
相关问题 更多 >
编程相关推荐