tags:

views:

55

answers:

2

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?

A: 

Don't match (split?) the space. Instead, match its negative.

(?:(?:\[[^\]]*\])|\S)+

This is not designed to match all CSS selectors, just your example. You should get a CSS parser for reliable results.

KennyTM
actually this detects all spaces for me if i try it on http://www.regextester.com/index2.html
Philipp Andre
A: 

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.

Daniel Brückner
@Daniel - _(?![^[\]]*]) is exactly what I am using. It can assert that the _ does not have following ], but it can assert that it does not have preceding [. But anyway, it servers the propose. I just want to know how to combine lookahead assertion with lookbehide assertion
powerboy
The negative look behind assertion for no opening bracket has to be something like (?<!\\[[^[\\]]*) but if I correctly remember using + and * is not allowed in look behind assertions. In consequence it is probably not possible to use a negative look behind assertion to check for no opening bracket.
Daniel Brückner