Solved

Regular Expressions find/replace needed

Posted on 2015-02-13
10
142 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

813 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

18 Experts available now in Live!

Get 1:1 Help Now