简体   繁体   中英

Match all (+) symbols between strings

I'm trying to catch this on Sublime Text.

Have this:

-webkit-calc(50%+4px/2+32px-32px*23%)

Want to catch all the + and - symbols inside calc so I can add a space between them...

Like this :

-webkit-calc(50% + 4px/2 + 32px - 32px*23%)

I have this already but caches all content inside calc(***) but just want all the + and -

(?<=calc\()(.+)(?=\))

Hope you can help me on this one guys,

Thanks in advance

Your (?<=calc\\()(.+)(?=\\)) does not work because it only grabs what is in-between calc( and the last ) on the line. You need to match + or - only inside those delimiters, and that is where \\G operator comes in.

If there are no nested parentheses, you can use

(?:calc\(|(?!^)\G)[^)+-]*\K[-+]

Replace with $0 . For more safety, add a word boundary before calc : \\bcalc .

See the regex demo

在此输入图像描述 V

在此输入图像描述

The pattern matches:

  • (?:calc\\(|(?!^)\\G) - matches calc( (matched with calc\\( or \\bcalc\\( ) or the end of the previous successful match (with (?!^)\\G )
  • [^)+-]* - 0+ characters other than ) , + and -
  • \\K - omits the characters matched so far
  • [-+] - match either - or +

If you want to only add spaces where they are missing, you could just match all spaces around the operators and replace them with single spaces:

(?:calc\(|(?!^)\G)[^)+-]*?\K\s*([-+])\s*
                             ^^^    ^^^^

And replace with $1 . This will turn -webkit-calc(50%+ 4px/2 +32px - 32px*23%) to -webkit-calc(50% + 4px/2 + 32px - 32px*23%) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM