0

I have regex which reads:

@"<img\s*[^>]*>(?:\s*?</img>)?

Can someone please explain this part: (?:\s*?)?

What is that?

mrblah
  • 93,893
  • 138
  • 301
  • 415

4 Answers4

9

match but don't capture any number of whitespace followed by a close image tag, zero or one times:

(?: = match but don't capture

\s*? = any number of whitespace (not greedy)

</img> = close image tag

)? = zero or one times

:)

Luke Schafer
  • 9,111
  • 2
  • 27
  • 28
1

(?:\s*?) selects any whitespace, if it exists, after the image tag. The ?: at the beginning tells the regex engine to not capture that group (meaning it won't be returned in the matches array)

brianreavis
  • 11,422
  • 3
  • 40
  • 50
0

non-capturing group of any number of whitespace characters, followed by a closing img tag

benPearce
  • 36,402
  • 14
  • 63
  • 95
0

The entire expression will capture any <img> tags that have corresponding </img> tags (but it won't capture the close tags). It doesn't capture the close tags because the (?:) syntax means "match but don't capture".

Some restrictions that are part of this regex:

  1. The \s* in the opening tag is redundant because [^>]* will capture this too
  2. Only whitespace is allowed between the opening and closing tags

Some examples:

  • <img> will not match
  • <img></img> will match, but only capture <img>
  • <img attr="123"></img> will match, but only capture <img attr="123">
  • <imgabc></img> will not match
  • <img> </img> will match, but only capture <img>
  • <img>ab</img> will not match

I highly recommend the Regular Expression Designer available for free at www.radsoftware.com.au for testing regexs

Tatham Oddie
  • 4,280
  • 18
  • 30