1

I'm trying to grab each individual keyframes declaration in a css file, and copy it, but inserting moz/ms/o to handle each browser with keyframes.

I'm using this regex:

(@)(-webkit-)([\s\S]*)(\}\R\}\R@)

To try and capture each collection (see full example at my Rubular)

Kirill Polishchuk
  • 51,053
  • 10
  • 118
  • 119
Riveascore
  • 1,536
  • 3
  • 22
  • 43

2 Answers2

2

Try this:

/(@)(-webkit-)(.*?\R\})/m

The m modifier makes it a multi-line regexp, so . matches across newlines. I removed the match for @ at the end, because then it can't match the last block in the file. And *? makes the match non-greedy, so it only matches one block at a time.

Rubular

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • @Charlesism The OP's regular expression seems to assume very specific layout of the CSS, so I assumed he was working with a layout that he can ensure matches the expectations. Regular expressions are not good at matching balanced braces, so this is probably the best you can do. – Barmar Oct 18 '13 at 02:45
0

The closest you get is...

(@-webkit-[^}]*}\s*to\s*{[^}]*}\s*})

...which can handle unusual/mangled indention in your CSS files decently. This is how it works:


  1. ( Start a capture group...

  2. @-webkit- ...upon this phrase.

  3. [^}]* } Continue until you you see a '}' character.

  4. \s* to \s* { Next, the phrase ' to ', followed by '{'...

  5. [^}]* } ...keep going till the next '}' character.

  6. \s* } A final '}' character, possibly preceded by whitespace.

  7. ) Stop capturing.


It might be that there are cases where you have a false positive since regex doesn't understand nesting.

Community
  • 1
  • 1
original_username
  • 2,330
  • 18
  • 24