Reputation: 19967
The following regex:
(\d+\.?\d+?)px \/\* (\d+) \*\/
matches 5.5px /* 1440 */
in:
position: relative;
padding: 5.5px /* 1440 */ 9px /* 1440 */;
border: 0;
but not 9px /* 1440 */
.
This regex does match both 5.5px /* 1440 */
and 9px /* 1440 */
but captures groups I do not wish to capture.
(\d+(\.)?(\d+)?)px \/\* (\d+) \*\/
How can I write (\d+(\.)?(\d+)?)px \/\* (\d+) \*\/
without capturing (\.)
and (\d+)
?
Upvotes: 0
Views: 135
Reputation: 22817
As per my original comment...
Your regex is saying:
\d+
Match any digit one or more times\.?
Match the dot character .
literally zero or one time\d+?
Match one or more digits, but as few as possible (lazy quantifier).Realistically, the error is on the last line and changing +?
to *
should fix your issue, however, the following approach is usually regarded as the better method to catching decimal point numbers as it doesn't allow your regex to catch numbers ending in decimal points without number following it (i.e. 10.
- this will be matched by \d+\.?\d*
, but it will not be matched by \d+(?:\.\d+)?
)
(\d+(?:\.\d+)?)px \/\* (\d+) \*\/
The regex above uses \d+(?:\.\d+)?
\d+
Match one or more digits(?:\.\d+)?
Match the following zero or one time
\.
Match the dot character .
literally\d+
Match one or more digitsUpvotes: 2
Reputation: 4694
\d+?
is a lazy version of \d+
, NOT an optional \d+
.
Instead, the *
quantifier means 0 or more.
You can also use the (?:…)
non-capture syntax.
Upvotes: 2