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 October 13th, 2008, 02:58 AM
m4st3rm1nd m4st3rm1nd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Location: USA
Posts: 42 m4st3rm1nd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 33 m 37 sec
Reputation Power: 5
Quoted String regex

Hi people,

I want to match and pick up quoted strings from html text. (but not the ones in the html tags)

( '([^\\']|\\.)*' | "([^\\"]|\\.)*" ) <- does the job of selecting quoted strings, first part for single-quoted and second half for double quoted.

But it also picks up the html tag properties.

eg. <p class="strong"> Here its mostly sunny. But it is raining outside.</p>
<span id="new" class="strong"> What you see now is "Some quoted text". This is 'single-quoted text'.</span>

The regex will match "strong","new" also. which I dont want. Any ideas how to modify the regex?

Reply With Quote
  #2  
Old October 13th, 2008, 04:29 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 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 4 h 54 m 57 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
Just strip out the tags beforehand. PHP has a strip_tags function for that exact purpose.
If your language doesn't have something similar replace /<[^>]*>/ with nothing.

Reply With Quote
  #3  
Old October 13th, 2008, 05:37 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 36 m 34 sec
Reputation Power: 3885
Also don't forget the HTML character entities for quotes too - &quot; is for double quotes. I don't remember singles off the top of my head.

Last edited by ishnid : October 13th, 2008 at 07:15 AM.

Reply With Quote
  #4  
Old October 13th, 2008, 05:46 AM
ryon420's Avatar
ryon420 ryon420 is offline
Permanently Banned
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jun 2006
Location: In a whale
Posts: 4,147 ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)ryon420 User rank is General 35th Grade (Above 100000 Reputation Level)  Folding Points: 1496 Folding Title: Novice Folder
Time spent in forums: 9 Months 2 Days 9 h 44 m 16 sec
Warnings Level: 10
Reputation Power: 0
Send a message via MSN to ryon420 Send a message via Google Talk to ryon420
Quote:
I don't remember singles off the top of my head.


& #39; & apos; at times as well (certain browsers don't like this one, though).

Reply With Quote
  #5  
Old October 13th, 2008, 11:13 AM
m4st3rm1nd m4st3rm1nd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Location: USA
Posts: 42 m4st3rm1nd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 33 m 37 sec
Reputation Power: 5
Thank you all for replies.

@ requinix : My language is Perl. And sorry, I dont get what you are trying to say.

I want to be able to match quoted strings other than the ones in the HTML tags. Even if I strip of the tags, the attribute values will match the regex.

@ishnid and ryon420: Yeah I will keep the html entities in mind.

Reply With Quote
  #6  
Old October 13th, 2008, 11:29 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 36 m 34 sec
Reputation Power: 3885
Quote:
Originally Posted by m4st3rm1nd
@ requinix : My language is Perl. And sorry, I dont get what you are trying to say.

I want to be able to match quoted strings other than the ones in the HTML tags. Even if I strip of the tags, the attribute values will match the regex.

If you strip out the tags, the attribute values won't be there anymore, so they can't possibly match.

Reply With Quote
  #7  
Old October 13th, 2008, 12:33 PM
m4st3rm1nd m4st3rm1nd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Location: USA
Posts: 42 m4st3rm1nd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 33 m 37 sec
Reputation Power: 5
oh that's right. got it. dont know what i was thinking earlier. i aint a morning person. You can tell

Reply With Quote
  #8  
Old October 14th, 2008, 08:23 AM
printf printf is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2005
Posts: 1,586 printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level)printf User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 4 Weeks 1 h 34 m 20 sec
Reputation Power: 274
Something like...

PHP Code:
<?php

$str 
'eg. <p class="strong"> Here its mostly sunny. But it is raining outside.</p>
<span id="new" class="strong"> What you see now is "Some quoted text". This is \'single-quoted text\'.</span>'
;

preg_match_all "/(?![^<]+>)(?:\"|')(.+)(?:\"|')/U"$str$out );

print_r $out[1] );

?>



Or if you don't want to match inside </a> tags then it would be..


PHP Code:
 preg_match_all "/(?!(?:[^<]+>|[^>]+\<\/a\>))(?:\"|')(.+)(?:\"|')/U"$str$out ); 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Quoted String 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