1878castle
1878castle

Reputation: 61

How to match a string with double and/or single dash?

With the following code, I would like to use regex to be able to select .ptress--a AND .ptress--a-l, and the other classess too.

.ptres--a { pointer-events: auto }
.ptres--n { pointer-events: none }

 @media (max-width: 1280px) {
    .ptres--a-l { pointer-events: auto }
    .ptres--n-l { pointer-events: none }
 }  

 @media (max-width: 840px) {
    .ptres--a-m { pointer-events: auto }
    .ptres--n-m { pointer-events: none }
 }

 @media (max-width: 400px) {
    .ptres--a-s { pointer-events: auto }
    .ptres--n-s { pointer-events: none }
 }

The regex that I have \w[\w]*(?:--\w+) doesn't match the whole string, leaving out the final -l|m|s part.

Upvotes: 0

Views: 41

Answers (1)

Ramaraja
Ramaraja

Reputation: 2626

Here's a simple regex for your requirement,

\.\w+--\w+-\w+|.\w+--\w+

This uses OR to match with either string of the format .ptres--a-l OR .ptres--a

You can use it in grep like this,

cat custom.css | grep -E -o '.\w+--\w+-\w+|\w+--\w+'

Upvotes: 2

Related Questions