powerboy
powerboy

Reputation: 10981

regex: match white-spaces that do not enclosed in []

For example, for this string,

div.img-wrapper img[title="Hello world"]

I want to match the first space but not the second space (which is enclosed in []). What is the regex?

Upvotes: 4

Views: 995

Answers (1)

Daniel Brückner
Daniel Brückner

Reputation: 59705

The following expression will do the job by using a look ahead assertion.

_(?>[^[\]]*(\[|$))

The underscore represents a space. This expression does not support nested brackets because regular expression are not powerful enough to express nested matched structures.

_          Match the space and
(?>        assert that it is not inside brackets
  [^[\]]*  by matching all characters except brackets
  (        followed by either
    \[     an opening bracket (a space inside brackets
           will have a closing bracket at this position)
    |      or
    $      or no more characters (end of line).
  )
)

UPDATE

Here is another (and more beautiful) solution using a negative look ahead assertion.

_(?![^[\]]*])

It asserts that the next bracket after a space is not a closing bracket.

Upvotes: 4

Related Questions