gpt4 book ai didi

python - 正则表达式向前看和向后看

转载 作者:行者123 更新时间:2023-12-04 07:25:09 26 4
gpt4 key购买 nike

我正在尝试正则表达式使用前瞻和后视向后提取数据。
在下面,我只对 column store error 感兴趣,即模式为 : search table error: , 我需要提取字符串直到上一个 : .

Error processing. Reason: Exception: Job aborted due to failure: xxxxx (asasdasd): com.db.jdbc.exceptions.JDBCDriverException: DBTech JDBC: [2048]: column store error: search table error:  [123]
我目前被困在 (?<=:)(.*?)(?=(: search table error)) .这是从第一次出现 : 中提取的从开始。
感谢您的任何帮助。

最佳答案


:\s*([^:]*?)\s*: search table error
regex proof .
说明
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^:]*? any character except: ':' (0 or more
times (matching the least amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
: search table error ': search table error'
Python code :
import re
regex = r":\s*([^:]*?)\s*: search table error"
test_str = "Error processing. Reason: Exception: Job aborted due to failure: xxxxx (asasdasd): com.db.jdbc.exceptions.JDBCDriverException: DBTech JDBC: [2048]: column store error: search table error: [123]"
match = re.search(regex, test_str)
if match:
print(match.group(1))
结果 : column store error
还:
(?<=:)[^:]*(?=:\s+search\s+table\s+error:)
this regex proof .
说明
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
[^:]* any character except: ':' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
search 'search'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
table 'table'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
error: 'error:'
--------------------------------------------------------------------------------
) end of look-ahead
Python code 喜欢
import re
regex = r"(?<=:)[^:]*(?=:\s+search\s+table\s+error:)"
test_str = "Error processing. Reason: Exception: Job aborted due to failure: xxxxx (asasdasd): com.db.jdbc.exceptions.JDBCDriverException: DBTech JDBC: [2048]: column store error: search table error: [123]"
match = re.search(regex, test_str)
if match:
print(match.group().strip())

关于python - 正则表达式向前看和向后看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68248512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com