The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Need to match last occurrence of one of two characters with regex
Discuss Need to match last occurrence of one of two characters with regex in the Regex Programming forum on Dev Shed. Need to match last occurrence of one of two characters with regex Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 15th, 2012, 02:04 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 2
Time spent in forums: 19 m 27 sec
Reputation 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.
Any help is appreciated, thank you!
Last edited by robmarston : August 15th, 2012 at 02:05 PM.
Reason: Typo
|

August 15th, 2012, 03:27 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
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.
|

August 15th, 2012, 06:26 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
See if this will work for you:
Code:
(.+?):(.+?);( \*/|\*/)?
|

August 16th, 2012, 03:18 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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.
|

August 16th, 2012, 12:59 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 2
Time spent in forums: 19 m 27 sec
Reputation 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.
|

August 16th, 2012, 05:15 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | 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).
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|