3 回答
TA贡献1804条经验 获得超7个赞
您可以使用类似的东西
s = "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier. (8169) (SQLExecDirectW)"print(s[s.rfind(']')+1: s.find('(')])
TA贡献1898条经验 获得超8个赞
如果要在较长的文本中找到此模式的多个实例,@mohammedwazeems的解决方案将不再有效。
在这种情况下,请使用正则表达式:
import re
regex = r".*](.+?)[(]" # avoid all until last ] that is followed by captured anything lazyly
# that is followed by an open (
log_file = """some text that is fine
[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier. (8169) (SQLExecDirectW)
more ok text
[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Fix me error. (8169) (SQLExecDirectW)
more okish text"""
matches = re.finditer(regex, log_file, re.MULTILINE)
for match in matches:
if len(match.groups())>0:
print ( match.group(1))
指纹:
Conversion failed when converting from a character string to uniqueidentifier.
Fix me error.
在这里测试:https://regex101.com/r/GPWs2a/1
TA贡献1856条经验 获得超17个赞
试试这个:
error = '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier. (8169) (SQLExecDirectW)'error[error.rfind(']')+1:error.find('(')]
这应该给出:
'Conversion failed when converting from a character string to uniqueidentifier. '
添加回答
举报