user3535807
user3535807

Reputation: 268

Need regex to remove unnecessary strings in Notepad++

I have big css file and need regex(Notepad++) to get only elements and css selectors found by specific css value. In following example I need to get element and selector by value 123456

header #objectnav nav a {
  border-right: solid 1px #c0c0c0;
  border-left: solid 1px #f4f9ff;
  color: #123456;
}

a:hover {
  color: #654321;
}

#hints .hint {
  background-color: #f4f9ff;
  border: 1px solid #e0f0ff;
  color: #123456;
  margin: 0 0 30px 0;
  position: relative;
}

on exit I expect following

header #objectnav nav a
color

#hints .hint
color

or, if possible

header #objectnav nav a^color
#hints .hint^color

Upvotes: 0

Views: 82

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51430

I did this just for the challenge:

The following regex will find all the rules containing the text 123456 as a value:

[^{}\s][^{}]*\{[^}]*?[-\w]+\s*:[^;}]*?123456[^}]*\}

But that's just a basic regex. The more challenging part is that I wondered if it's possible to generate a report such as the one you asked for using nothing but Notepad++. It turns out it's possible.

Replace the following pattern:

\s*([^{}]+?)\s*\{[^}]*?(?(?=([-\w]+)\s*:[^;}]*?123456)[^}]*|[^}])*\}\s*

With the following replacement string:

(?2$1^$2:)

Or this one depending on the output you prefer:

(?2$1\r\n$2\r\n\r\n:)

I didn't test it extensively but it works for the test cases you provided.

Upvotes: 1

Related Questions