Solved

Regular Expressions find/replace needed

Posted on 2015-02-13
10
138 Views
Last Modified: 2015-02-20
I am trying to avoid a huge manual find replace task by using regular expressions. Hopefully, due to the layout, I can pull this off. Here is what I have:

Old code

<span class="dropt" title="">[i]define this word[/i]<span style="width:300px">[i]here is the definition that will be shown in a pop up</span></span>[/i]

Open in new window


New code

<a href="#" title="<span class='red'>[i]here is the definition that will be shown in a pop up[/i]<a href='#'></a></span>">[i]define this word[/i]</a>

Open in new window


Obviously because there is a jockeying around of the position of the text that remains, cant be done with a basic search replace. There are about 100 or so instances of this with varying definitions in the document.

Thank you
0
Comment
Question by:mcpilot1
  • 5
  • 5
10 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40607891
$regex = [regex] '(?m)^<span.*?\[i\](.*?)\[/i\].*?\[i\](.*?)</span></span>\[/i\]$'
$result = $regex.Replace($subject, '<a href="#" title="<span class=''red''>[i]$2[/i]<a href=''#''></a></span>">[i]$1[/i]</a>')

Open in new window

HTH,
Dan
0
 

Author Comment

by:mcpilot1
ID: 40607942
Dan, sorry for the confusion - I only added the to highlight the actual text in the code but there is no italic or emphasis. This is an examlpe of the actual code:

<span class="dropt" title="">September 11th Victim Compensation Fund
<span style="width:300px;"><b>September 11th Victim Compensation Fund: </b> A program administered by the U.S. Department of Justice that provides compensation for economic and non-economic loss to 

individuals or relatives of deceased individuals who were killed, physically injured, or made physically ill as a result of the terrorist-related aircraft crashes of September 11, 2001.</span>
</span>

Open in new window


does that change the regex formula?

Thanks,

Pat
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40608300
A bit. Try this:
$regex = [regex] '(?sm)^<span class="dropt" title="">(.*?)<span style="width:300px;">(.*?)</span>.*?</span>$'
$result = $regex.Replace($subject, '<a href="#" title="<span class=''red''>$2<a href=''#''></a></span>">$1</a>')

Open in new window

0
 

Author Comment

by:mcpilot1
ID: 40614229
I am getting a "regular expression syntax error". I am attaching the HTML file that I am workign with. Can you have a look?
handbook-working.html
0
 

Author Comment

by:mcpilot1
ID: 40618642
here is the error message when I try to run this in Dreamweaver:

Error png
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40619786
For some reason, I was sure you're trying to do the find/replace in Powershell, so I gave you Powershell code.

I don't have Dreamweaver, but I'll try and send you the instructions for Notepad++ (it's a free and very powerful text editor).
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40619906
Here is the expression that seems to work in Notepad++:
Find what: <span class="dropt" title="">(.*?)<span style="width:300px;">(.*?)</span>.*?</span>
Replace with: <a href="#" title="<span class='red'>$2<a href='#'></a></span>">$1</a>

Open in new window

Notepad   s
0
 

Author Comment

by:mcpilot1
ID: 40621069
YES! It worked. I had to get v 6.x of the Notepad++ with the "matches newline" but once I did that, it worked. Thank you for your persistence. I really appreciate it.

Pat
0
 

Author Closing Comment

by:mcpilot1
ID: 40621071
Dan was relentless in his assistance and got me a solution in the end. Thank you Dan!
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40621182
You're welcome.

Glad I could help!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
I still run into .cgi files every now and then. In some instances, I actually prefer the simplicity of a .cgi script to other options. Since I use DreamWeaver extensively, what I needed was a way to open .cgi scripts in Dreamweaver. And I wanted to …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now