问题
I have a long regexp pattern that needs to be matched in TCL
regexp {.*%MKA-5-SESSION_START: +\((.*) +:.*\) +MKA Session started for.*([0-9A-Fa-f]{4}.[0-9A-Fa-f]{4}.[0-9A-Fa-f]{4}).*AuditSessionID +(.*), +AuthMgr-Handle +(.*)} $op - intf mac auditsessid authmgrhdl
How can i split this to multiple lines , splitting using \ is not working as regexp fails.
Thanks, Agnel.
回答1:
That is a monster RE. The easiest way to split it up would be to use an extended mode RE by putting (?x)
at the start of it, which makes whitespace meaningless in the RE (you then also have to use \s
to match real whitespace)
It's also useful in this situation to put the RE in its own variable. It's just clearer.
set RE {(?x)
# We can use comments in extended mode too! This is useful for sanity's sake…
.*
# Detect and match for 'intf'
%MKA-5-SESSION_START:\s+\((.*)\s+:.*\)\s+
# Detect and match for 'mac'
MKA\sSession\sstarted\sfor.*([0-9A-Fa-f]{4}.[0-9A-Fa-f]{4}.[0-9A-Fa-f]{4}).*
# Detect and match for 'auditsessid'
AuditSessionID\s+(.*),\s+
# Detect and match for 'authmgrhdl'
AuthMgr-Handle\s+(.*)
}
Then you just match it as normal, with regexp
, which is clearer now that we've separated the RE itself out:
regexp $RE $op -> intf mac auditsessid authmgrhdl
It's usually a good idea to check the result of regexp
to see if things matched.
来源:https://stackoverflow.com/questions/35791814/regexp-pattern-across-multiple-lines