August 15th, 2012, 03:04 PM
Need to match last occurrence of one of two characters with regex
I need to match last occurrence of one of two characters with regular expression. In this instance it's either a semi-colon or the close of a comment. I'm using PHP if that matters.
Consider the string:
My goal is to match each CSS property regardless of whether it is commented-out or not. The trouble is, I'm not sure how to write a pattern that would match both normal properties ending with semi-colons and properties that have been commented-out.
/* background-image:url('http://www.example.com/image.jpg'); */
The pattern below isolates each property but picks up on the semi-colon only, not the close of a comment.
Any help is appreciated, thank you!
Last edited by robmarston; August 15th, 2012 at 03:05 PM.
August 15th, 2012, 04:27 PM
You want to capture the fact that the property was commented too?
Also, let me provide a larger example text:
(unless you know something about the CSS that we don't)
/* disable these: let the normal white background happen
background-image: url('http://www.example.com/image.jpg'); */
font-family: Helvetica, serif;
Last edited by requinix; August 15th, 2012 at 04:31 PM.
August 15th, 2012, 07:26 PM
See if this will work for you:
August 16th, 2012, 04:18 AM
I'm not sure if this task really makes sense. Comments by definition don't require any specific structure, so you can easily run into trouble when trying to pull out rules.
What if the comment is complete "garbage" and only by accident contains a rule-like syntax?
Apart from that, processing data formats should always be done with an actual parser. A 5 second search has got me this one:
Regular expressions are only the last resort for this kind of problem.
August 16th, 2012, 01:59 PM
@requinix I want to capture all the CSS properties as matches whether they are commented out by the user who wrote the CSS or not.
@spacebar208 I'll give it a shot, thank you.
@Jacques1 You're right, it's very possible the user could input a garbage comment but for my project it doesn't matter -- it'd only hurt them in the long run. I'm not evaluating, storing, or running any other processes on the string so I don't really care if their formatting is incorrect. If you drill down into the parser library you recommended they're using Regex as well. It's a bit of overkill considering my script is only a few lines but I'm sure it's the perfect solution for more complex tasks -- thanks for looking into it for me.
August 16th, 2012, 06:15 PM
The thing is that a parser allows you to concentrate on the actual problem instead of fumbling with syntax details.
Originally Posted by robmarston
If you'd downloaded the library, you probably were finished in 10 minutes or so. But now you're still struggling with different regexes which all only halfway work.
I must say that I used to think just like you and always tried to avoid the overhead of a library. But now I think it's more important to choose the right tool.
Regexes are somewhat overused, because they seem to be the solution to almost any problem. But in fact they hardly make sense for anything else but searching or checking simple patterns (like a date or something).