3 回答

TA貢獻1804條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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('(')]
這應(yīng)該給出:
'Conversion failed when converting from a character string to uniqueidentifier. '
添加回答
舉報