Shivam Gupta Shivam Gupta - 1 year ago 66
Python Question

Need help in this Regular Expression

The regular expression I am using:


The string is:

"%sprix is the %s[4] online %s[3]:6 pshopping %s:9 in %s"

I am trying to find all
from string except

My output using the above regular expression is not giving expected results.

Expected Output is:

%s, %s:9, %s

My Output is:

%s, %s[ , %s[, %s:9, %s

Answer Source

You may use


See the regex demo.


  • %s - a percentage sign and s
  • (?!\[) - that is not followed with [
  • (?::\d+)? - an optional sequence of a : and one or more digits

The above regex will fail the match in all cases when %s is followed with [, e.g. in %s[text. If you only want to fail the match when %s is followed with [+number+], use %s(?!\[\d+\])(?::\d+)?.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download