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 February 19th, 2013, 09:42 PM
student89 student89 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 student89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 12 sec
Reputation Power: 0
Need help with HTML tags Regex

Ok so I have the following regex expression for identifying html tags:
[<a-z-A-Z-0-9-!@#$%^*&()"-:>]+

However, this highlights everything :/ please assist asap. Thanks!

Reply With Quote
  #2  
Old February 20th, 2013, 07:20 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,033 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 6 Days 20 h 57 m 29 sec
Reputation Power: 812
So :%! and 123 are valid HTML tags? That would surprise me ...

You regex just consist of repeating a character class, so any (non-empty) combination of those characters is considered valid. That obviously makes no sense. HTML tags usually look like this:
Code:
<h1>
</p>
<input type="text" name="password" />

But maybe you mean something different?

Note that processing HTML with regexes is a really, really bad idea 99% of the time -- although many people seem to love it. Contrary to popular belief, regexes are not an all-powerful parsing tool. They are in fact very limited and can only parse subsets HTML. So whenever you find yourself trying to parse HTML with regexes, step back and consider using a different approach. Every mainstream language has specialized HTML parsers for exactly that purpose.

Reply With Quote
  #3  
Old February 20th, 2013, 11:45 AM
student89 student89 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 student89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 12 sec
Reputation Power: 0
How would I modify this?

I am trying to identify all html tags that have characters, numbers, or symbols between them. how would i do that?

Reply With Quote
  #4  
Old February 20th, 2013, 04:02 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 544 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 5 Days 2 h 25 m 5 sec
Reputation Power: 406
As Jacques said, it is almost always a bad idea to use regexes to try to parse HTML (or XML, for that matter).

If you really want to go this way (which could possibly possibly be tolerated for extremely simple operations), you could try something like this:

Code:
<[^>]+>


which means an opening <, followed by a number of anything but a closing >, followed by a closing >.

This is simplistic, but at least it will not consider this:

Code:
<center><b><font face="Verdana">Foo Bar </font></b></center>


as one single long tag starting with the opening < at the beginning of the line and the closing > at the end of the line above, but will be more or less able to match tags individually.

However, this will break, for example, if the tag spans over more than one line or in many other circumstances. In brief, don't do that except possibly as a one-shot script for extremely simple substitutions.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Need help with HTML tags 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