1

Need to match pattern: [[ : ]],

I do: \[\[(?!\[\[).[:]+.*?\]\]

But when pattern have environment with many same patterns like this: [[:]] [[ [] ]] [[]] [[ : ]] [[ : ]] matching is fail, and return: [[ : ]], [[ : ]]

Or if regex is: \[\[.*?[:]+.*?\]\] , giving the:

[[  []  ]] [[]] [[ :  ]] instead of [[ :  ]]

Example

What is the true way?

VadimCh
  • 71
  • 1
  • 9
  • Can you elaborate on the pattern that you need to match? If you simply need to match the pattern `[[ : ]]` then you could just use `\[\[ : \]\]` – CAustin Feb 06 '19 at 20:31
  • Maybe `\[\[[^][:]*:[^][]*]]`? Or can your `[[ : ]]` contain single `[` and `]` inside? Then you need `\[\[(?:(?!\[\[|]])[^:])*:.*?]]` ([demo](https://regex101.com/r/qvvctf/1)). – Wiktor Stribiżew Feb 06 '19 at 20:32

1 Answers1

1

When you have to match [[ : ]]-like strings you should match [[ first, then any 0+ chars that are not : and do not start a leading/closing sequence, that is, [[ and ]], then you need to match a : char, and then any 0+ chars up to the first (leftmost) occurrence of ]].

The pattern you may use is

\[\[(?:(?!\[\[|]])[^:])*:.*?]]

See the regex demo. Remember to use re.DOTALL or re.S for the . to match across multiple lines.

Details

  • \[\[ - a [[ substring
  • (?:(?!\[\[|]])[^:])* - a tempered greedy token matching any char other than a : (see [^:] negated character class), 0+ times (see *), that does not start a [[ and ]] sequences
  • : - a colon
  • .*? - any 0+ chars, as few as possible
  • ]] - a ]] substring.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563