#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    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:

    Code:
    body {
    /* background-image:url('http://www.example.com/image.jpg'); */
    height:100%;
    width:100%;
    }
    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.

    The pattern below isolates each property but picks up on the semi-colon only, not the close of a comment.

    Code:
    /[^{]*?;/
    Any help is appreciated, thank you!
    Last edited by robmarston; August 15th, 2012 at 02:05 PM. Reason: Typo
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,984
    Rep Power
    9397
    You want to capture the fact that the property was commented too?

    Also, let me provide a larger example text:
    Code:
    body {
    	/* disable these: let the normal white background happen
    	background-color: gray;
    	background-image: url('http://www.example.com/image.jpg'); */
    	font-family: Helvetica, serif;
    	height: 100%;
    	width: 100%;
    }
    (unless you know something about the CSS that we don't)
    Last edited by requinix; August 15th, 2012 at 03:31 PM.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    See if this will work for you:
    Code:
    (.+?):(.+?);( \*/|\*/)?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    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:
    https://github.com/sabberworm/PHP-CSS-Parser/
    Regular expressions are only the last resort for this kind of problem.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0
    @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.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by robmarston
    @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.
    The thing is that a parser allows you to concentrate on the actual problem instead of fumbling with syntax details.

    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).

IMN logo majestic logo threadwatch logo seochat tools logo