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
|
RegEx: how to exclude a tag attribute from a match?
Discuss RegEx: how to exclude a tag attribute from a match? in the Regex Programming forum on Dev Shed. RegEx: how to exclude a tag attribute from a match? 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:
|
|
|

April 27th, 2012, 08:46 AM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 3 m 31 sec
Reputation Power: 0
|
|
|
RegEx: how to exclude a tag attribute from a match?
Hello to everybody.
I am not a programmer but a designer, and I am completing a purge of a big database of html files full of usefull tags.
I already know (it was suggested to me) how to strip a specific pair of tag with a specific attribute (class="ArticoloTesto"):
find: (?s)<p[ \t\r\n]+class="ArticoloTesto">(.+?)</p>
replace: \1
Now shoud be nice to find another two commands, if you can please help me. :-)
First:
The opposite of the mentionen command. I mean a way to strip all the P pair tags without the attribute class="ArticoloTesto". So strip P pair with other attribute + P pair without any attribute. All preserving the inside text.
So for exampre an original strings like:
<p class="OtherClass">Disposizioni sulle acque</p>
<p class="ArticoloTesto">Fonte: BOLLETTINO UFFICIALE<br>
DELLA REGIONE TRENTINO</p>
<p>test text</p>
Should became:
Disposizioni sulle acque
<p class="ArticoloTesto">Fonte: BOLLETTINO UFFICIALE<br>
DELLA REGIONE TRENTINO</p>
test text
Second:
How to replace a specific pair tag, with and without a specific attribute, with another tag?
Ex:
from: <span class="ArticoloTesto">same text</span>
to: <p class="ArticoloTesto">same text</p>
Thank you in advance for any of your replay, anyway.
ps: I am useing this command on advanced find and replace software based on a perl engine regex. Ultra Edit in particulary.
Thank you so much.
Raf.
|

April 28th, 2012, 12:32 AM
|
|
|
Hello, Raf,
1) <p> tag without "ArticoloTesto" class:
Code:
find: <p(?![ \t\r\n]+class="ArticoloTesto").*?>(.+?)</p>
replace: \1
I'm not sure if UltraEdit supports lookahead, but it works in PHP and in my search-and-replace tool.
2) change <span class="ArticoloTesto"> to <p class="ArticoloTesto">
Code:
find: <span[ \t\r\n]+class="ArticoloTesto">(.+?)</span> (the same as in your example)
replace: <p class="ArticoloTesto">\1</p>
|

April 30th, 2012, 05:14 AM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 3 m 31 sec
Reputation Power: 0
|
|
Thank you very much, abareplace :-)
I was able answer my second question.. I realize it some minutes after my post but ...
...your answer of my first, most important, question was so so utile!!
<p(?![ \t\r\n]+class="ArticoloTesto").*?>(.+?)</p>
Great!
Thanks one more time ;-)
Raf.
|

April 30th, 2012, 05:52 AM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 3 m 31 sec
Reputation Power: 0
|
|
|
How to copy text between a tag and paste into another tag.
A last question, if I am not bothering you.
I really have quite finished the work, but I still want some step forward.
I have for example this HTML tag:
<title>alwaysthesame</title>
and later
<p class="ArticoloTitoloPrincipale">different text for each file</p>
I would like to copy different text for each file and paste it over alwaysthesame.
In that way I can have a title corresponding to the text inside the only one tag p class="ArticoloTitoloPrincipale" of the page (wich is the title of the document, as you can imagine).
If it's not too complex, I wonder to can correct the absence of a corresponding title in each of my html files.
Thank you in any case :-)
Raf.
|

April 30th, 2012, 06:04 AM
|
|
|
Please try:
Code:
Search for: (?s)<title>.*?</title>(.*?)<p class="ArticoloTitoloPrincipale">(.*?)</p>
Replace to: <title>\2</title>\1<p class="ArticoloTitoloPrincipale">\2</p>
|

April 30th, 2012, 09:07 AM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 3 m 31 sec
Reputation Power: 0
|
|
Thank you so much!!!
Yes, abareplace.
It works perfectly!!
It's a very complex command for me (backreferences seems not easy..). I've tried a lot before asking help out there, but I really have no skills on it..  I am a graphic designer and photographer in Italy.
I have to admit that Perl knowlage gives a lot o freedom!!
My best regards.
Raffaele.
|
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
|
|
|
|
|