Pergunta

Consider two printk kind of function calls -

TRACE_BR(TRACE ,    "END. rc = %d\n", rc );

TRACE_BR(TRACE, "Value = %s", string );

I am writing a regex to match whole function calls like the above which have % inside the string argument inside them, but should NOT match if the string END is also inside.

I used negative lookahead like this

TRACE_BR\(TRACE.*?(?!END)%.*

I expect this regex to match only the second function call, but its matching the first one too.

I guess I am going wrong somewhere with the greedy * part.

Foi útil?

Solução

The regex should be:

TRACE_BR\(TRACE(?!.*END).*?%.*

This regex will not match the line if END is a substring appearing after TRACE. You may need to modify the regex if you want a more refined matching.

You can think of the regex as: after I matched TRACE (and etc. in front), from the current position, I would like to look ahead that I cannot find END substring.

In your regex, if the character after the negative look-ahead assertion is %, the assertion is always true as END substring can't start there; if the character after the negative look-ahead assertion is not %, the regex will fail to match and backtrack.

In the hypothetical case that you remove the %, the regex will still allow a string with END to pass, since the sequence of any character .*? will allow the negative look ahead to match at any position along the way, and it can just find a position that is not the start of the string END and match it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top