Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 15th, 2012, 02:04 PM
robmarston robmarston is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 robmarston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Code:
/[^{]*?;/


Any help is appreciated, thank you!

Last edited by robmarston : August 15th, 2012 at 02:05 PM. Reason: Typo

Reply With Quote
  #2  
Old August 15th, 2012, 03:27 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,680 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 1 h 55 m 14 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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.

Reply With Quote
  #3  
Old August 15th, 2012, 06:26 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 188 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 5 m
Reputation Power: 41
See if this will work for you:
Code:
(.+?):(.+?);( \*/|\*/)?

Reply With Quote
  #4  
Old August 16th, 2012, 03:18 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,835 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 4 h 14 m 14 sec
Reputation Power: 811
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.

Reply With Quote
  #5  
Old August 16th, 2012, 12:59 PM
robmarston robmarston is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 robmarston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old August 16th, 2012, 05:15 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,835 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 4 h 14 m 14 sec
Reputation Power: 811
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).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Need to match last occurrence of one of two characters with regex

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap