Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7979977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:55:42+00:00 2026-06-04T09:55:42+00:00

I thought this was going to be pretty simple, but I’ve been struggling with

  • 0

I thought this was going to be pretty simple, but I’ve been struggling with it now for a while. I know there are CSS parser classes out there that can acheive what I want to do… but I don’t need 95% of the functionality they have, so they’re not really feasible and would just be too heavy.

All I need to be able to do is pull out any class and/or ID names used in a CSS file via regex. Here’s the regex I thought would work, but hasn’t.

[^a-z0-9][\w]*(?=\s)

When run against my sample:

.stuffclass {
 color:#fff;
 background:url('blah.jpg');
}
.newclass{
 color:#fff;
 background:url('blah.jpg');
}
.oldclass {
 color:#fff;
 background:url('blah.jpg');
}
#blah.newclass {
 color:#fff;
 background:url('blah.jpg');
}
.oldclass#blah{
 color:#fff;
 background:url('blah.jpg');
}
.oldclass #blah {
 color:#fff;
 background:url('blah.jpg');
}
.oldclass .newclass {
 text-shadow:1px 1px 0 #fff;
 color:#fff;
 background:url('blah.jpg');
}
.oldclass:hover{
 color:#fff;
 background:url('blah.jpg');
}
.newclass:active {
 text-shadow:1px 1px 0 #000;
}

It does match most of what I want, but it’s also including the curly brackets and doesn’t match the ID’s. I need to match the ID’s and Classes separately when conjoined. So basically #blah.newclass would be 2 separate matches: #blah AND .newclass.

Any ideas?

===================

FINAL SOLUTION

I wound up using 2 regex to first strip out everything between { and }, then simply matched the selectors based on the remaining input.

Here’s a full working example:

//Grab contents of css file
$file = file_get_contents('css/style.css');

//Strip out everything between { and }
$pattern_one = '/(?<=\{)(.*?)(?=\})/s';

//Match any and all selectors (and pseudos)
$pattern_two = '/[\.|#][\w]([:\w]+?)+/';

//Run the first regex pattern on the input
$stripped = preg_replace($pattern_one, '', $file);

//Variable to hold results
$selectors = array();

//Run the second regex pattern on $stripped input
$matches = preg_match_all($pattern_two, $stripped, $selectors);

//Show the results
print_r(array_unique($selectors[0]));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:55:44+00:00Added an answer on June 4, 2026 at 9:55 am
    [^a-z0-9][\w]+(?=\s)
    

    I changed your * to a + match

    It works fine in RegEXR – an awesome regex development tool: http://gskinner.com/RegExr/ (See bottom right of window to download the desktop version)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Initially I thought this was going to work, but now I understand it won't
I am going crazy now. Googled this, thought some kind of IDE bug. Maybe
Ok, this has been an on-going annoyance, so naturally I thought to bring it
I thought this would be fairly simple but it turns out not to work
I thought this was covered elsewhere but I don't see it now. Anyway, having
I thought this would be pretty easy but I'm running into all sorts of
I've got a feeling this is going to be something really simple, but anyway.
Unfortunately this is going to be a pretty open-ended question, but I am at
I feel pretty stupid asking this, but as I don't know the answer, I'm
Im new on this project and am going to write, what i thought was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.