jsuissa
jsuissa

Reputation: 1762

Optional whitespaces inside lookbehind regex

Using Smarty template engine, but looking to pre-process HTML to change font-size across all CSS.

I want to make the whitespace(s) optional after the colon so it will match the number no matter what.

The issue is I can only seem to get optional matching as part of the expression, not the lookbehind.

RegEx: (?<=font-size: )[0-9]+

HTML excerpt:

body {
    font-family: 'Open Sans', sans-serif;
    font-size: 9pt; 
    height: 100%;
    min-height: 100%;
    display:block;    
}

Upvotes: 1

Views: 2275

Answers (2)

Qtax
Qtax

Reputation: 33908

Do not use lookbehinds when you can do it easily with a capturing group.

In your case you can do something like:

(\bfont-size:\s*)([0-9]+)

Then use the capturing groups $1 and $2 as you need.

Upvotes: 1

ZZ-bb
ZZ-bb

Reputation: 2167

Reg ex symbol for white space \s so is this what you need: (?<=font-size:\s?)[0-9]+?

Upvotes: 0

Related Questions