[英]Regex lookahead & lookbehind
我正在尝试正则表达式使用前瞻和后视向后提取数据。
在下面,我只对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
请参阅正则表达式证明。
解释
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\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'
蟒蛇代码:
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:)
请参阅此正则表达式证明。
解释
--------------------------------------------------------------------------------
(?<= 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 代码,例如
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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.