java - regular expressions with words in different lines -
i have following string:
>stat f -------------- begin of statistic 3 (f) activate context request tim eouts : 0 attach timeouts : 0 deactivate context request t imeouts : 0 detach timeouts : 0 gmm_attach accepts : 0 gmm_attach completes : 0 gmm_attach congestions : 0 gmm_attach rejects : 0 gmm_attach requests : 0 gmm_authentication failures : 0 gmm_authentication requests : 0 gmm_authentication responses: 0 gmm_detach requests : 0 gmm_detach responses : 0 gmm_id requests : 0 gmm_id responses : 0 gmm_protocol error unspecifi ed : 0 #more here key:value pairs -------------- end of statistic 3 < stat exec'd
i want extract key value pairs string. have created "ugly"-looking regular expression:
(^\w+\s*\w*\s*\w*\s*\w*\s*\w*\s*\w*\s*\w*\s*:\s*\d+)
it checks words , whitespaces etc, , works. more pretty way like: me lines don't start >stat f
or -
, don't start <
. tried this
(^[^><-].*)
but when key extends in 2 lines, matches them 2 different things. 1 match
activate context request tim
and 1
eouts : 0
is there way bypass behavior?
you can use following regex:
^([^>-][^:]*)\s*:\s*([^:\n]+)
as java string:
string pattern = "(?m)^([^>-][^:]*)\\s*:\\s*([^:\\n]+)";
see demo
[^>-]
makes sure not capture line starting >
or -
. capturing multiline names due [^:]
pattern.
a negated character class [^:]
means any character (even newline symbol) not :
. since :
acts delimiter, can match before it, , after that. adding \n
2nd negated character class matching values (e.g. 0
s) make sure stop @ newline, next line might start key name.
output:
Comments
Post a Comment