Solved

Regular Expressions find/replace needed

Posted on 2015-02-13
10
146 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Adobe Dreamweaver CS5 is a WYSIWYG web page editor that has advanced HTML, CSS, and Javascript rendering functionality and is probably the most well-known HTML editor available. Much of Dreamweaver's appeal centers around the Design View interfac…
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…

821 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