1

I'm trying to write a regex to find all ID selectors in a CSS file. Basically, that means any word that starts with a #, so okay

#\w+

Except ... color specifiers can also start with a #. So what I really want is all words that start with a # that are NOT between { and }. I can't figure out how to say this.

I'm doing this in Notepad++ so I need that flavor of regex.

BTW my real objective is to delete everything that's not an ID selector from the file, so I end up with just a list of selectors. My first try was

Find: [^#]*(#\w+)
Replace: \1\r\n

... and then hit Replace All.

But then I ran into the color problem.

Update

Someone asks for an example. Ok:

Input:

.foo {max-width: 500px;}
#bar {text-align: left;}
.splunge, #plugh {color: #ff0088;}

Desired output:

#bar
#plugh

Note the point is that it includes the two "pound strings" that come outside of braces but not the one that comes inside braces.

9
  • #\w+(?!\s*}) may work for you Commented Aug 9, 2018 at 21:04
  • Considering FFFFFF is a valid HTML ID, I don't see how you could possibly differentiate the two. What kind of ID's are you targeting? Are you specifically looking for ID's that are the target of a call to a method? (e.g. $("#myID"), document.querySelector("#myID"), etc.) knowing your goal or desired outcome may help us suggest alternative ideas. Commented Aug 9, 2018 at 21:05
  • @JosephMarikle Yes, I quickly realized I couldn't base the test on "looks like a valid color code". #FFFFFF may be an unlikely ID, but certainly not impossible. And, say, #BAD or #FACE02 could certainly turn up. That's why I was thinking that if it's between braces, than I could say it's not an ID and so not interesting. I guess that could skip media queries, now that I think about it. Commented Aug 9, 2018 at 21:17
  • @JosephMarikle My ultimate goal is that I have to find all CSS that references an ID, and that ID is in a Visual Basic user control. Possible to go at it from the other direction: find all the IDs in VB UCs and then search the CSS for them. I'm not sure that that would be any easier. Commented Aug 9, 2018 at 21:19
  • 1
    Could you show sample input and expected result? Commented Aug 10, 2018 at 8:19

1 Answer 1

3

What about this? You could use a lookahead expression:

#\w+(?=[^}]*?{)

It ensures that a { follows the match (indicating that the match is part of a selector), but not after a } character (excluding any matches against color declarations in the CSS).

  • #: match must begin with a #
  • \w+: match one or more word characters (might need tweaked. \w is equivalent to [A-Za-z0-9_])
  • (?=...): positive lookahead
  • [^}]*?: Any character not matching }
  • {: the { character

https://regex101.com/r/Di43hX/3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.