Solved

How to find and replace text between two words?

Posted on 2014-02-25
12
4,057 Views
Last Modified: 2014-03-11
hi

This is a my current data (extracted from html)
I need to delete everything between the first word(e.g AAAAAAA) and last word(e.g BBBBBBB) of each paragraph.
I wish the outcome to be

AAAAAAAA, BBBBBBBB

CCCCCCCCC,DDDDDDDD

EEEEEEEE, FFFFFFFFF

I will need export the data to Excel.
I tried to Google up and down, but cant find a way to select the data in between.
Hope you give a solution.
Thanks

----------------------------
Text need to be processed
----------------------------
AAAAAAAA</a></p><div class="box-listing_textCS6"><ol><li><div class="icon_mobileCS"></div><a id="csagentphone17556" style="color:#000; font-weight:bold;" class="csagentphonelead" href="findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=1000&st=PA&ct=&st1=&ct1=#17556" title="Click To Preview" data="BBBBBBBB

CCCCCCCCC</a></p><div class="box-listing_textCS6"><ol><li><div class="icon_mobileCS"></div><a id="csagentphone16127" style="color:#000; font-weight:bold;" class="csagentphonelead" href="findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=1000&st=PA&ct=&st1=&ct1=#16127" title="Click To Preview" data="DDDDDDDD

EEEEEEEE</a></p><div class="box-listing_textCS6"><ol><li><div class="icon_mobileCS"></div><a id="csagentphone15638" style="color:#000; font-weight:bold;" class="csagentphonelead" href="findanagent.aspx?ty=as&ak=&rk=&pg=1&rmp=1000&st=PA&ct=&st1=&ct1=#15638" title="Click To Preview" data="FFFFFFFFF
0
Comment
Question by:Shirley80
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 200 total points
ID: 39886381
Why use Word, that has only basic wildcard abilities, and not use a true Regex enabled editor?

For ex, it's trivial to do this in Notepad++.
delete htmlJust use
(\w*).*"(\w*)
in the find box and
$1,$2
in the replace box.

PS: make sure the cursor is at the beginning of the file.

HTH,
Dan
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 50 total points
ID: 39886423
Easiest would be to:

1) Put your data into Excel

2) Add this function to a regular VBA module:

Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True, _
    Optional MultiLine As Boolean = False)
    
    ' Function written by Patrick G. Matthews.  You may use and distribute this code freely,
    ' as long as you properly credit and attribute authorship and the URL of where you
    ' found the code
    
    ' This function relies on the VBScript version of Regular Expressions, and thus some of
    ' the functionality available in Perl and/or .Net may not be available.  The full extent
    ' of what functionality will be available on any given computer is based on which version
    ' of the VBScript runtime is installed on that computer
    
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll
    ' controls whether all instances of the matched string are replaced (True) or just the first
    ' instance (False)
    
    ' If you need to replace the Nth match, or a range of matches, then use RegExpReplaceRange
    ' instead
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    ' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
    ' where a large number of calls to this function are made, making RegX a static variable that
    ' preserves its state in between calls significantly improves performance
    
    Static RegX As Object
    
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
        .MultiLine = MultiLine
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    
End Function

Open in new window


3) Assuming your data start in A1, use a formula like this:

=RegExpReplace(A1,"^([A-Z]+)(.*?([A-Z]+))$","$1, $3")

I explain this approach in my article here: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
0
 
LVL 13

Expert Comment

by:Santosh Gupta
ID: 39886430
Hi,

use "Find and replace" and enter /a*data=" in the find box and. Now click on "Replace all"

after that again use "Find and replace" and enter < in the find box and , in the replace box. Now click on "Replace all"
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39886485
@patrick: your definition of "easy" is way different than mine :)

@sgupta1181: your solution works for the sample, but you have to check "Use wildcards". And if the html/xml code changes (so the end is no longer "data=") then it no longer works.
0
 
LVL 21

Assisted Solution

by:EricFletcher
EricFletcher earned 150 total points
ID: 39886518
Word's wildcards are not as elegant as a true Regex editor, but they are able to do what you want with Find and Replace.

Find what: (?[!<]{1,})(\<)(*" data=")(?[!^0013]{1,})
Replace with: \1,\4

This finds 4-part structures consisting of:
(1) any number of characters except <;
(2) the < character;
(3) any number of characters plus " data="; and
(4) any number of characters except the paragraph break (the ^0013).
Each element of the structure needs to be enclosed within parentheses within the Find definition so it can be referred to in the replace string.

It will then replace each found structure with the 1st and 4th elements separated by a comma.
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 92

Expert Comment

by:Patrick Matthews
ID: 39887131
@Dan,

That's because I hadn't seen your Notepad++ suggestion before I posted mine :)

Patrick
0
 
LVL 21

Assisted Solution

by:Ejgil Hedegaard
Ejgil Hedegaard earned 50 total points
ID: 39887132
With a formula in Excel, data in A1
=LEFT(A1,SEARCH("<",A1)-1)&","&RIGHT(A1,LEN(A1)-SEARCH(CHAR(255),SUBSTITUTE(A1,CHAR(34),CHAR(255),LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(34),"")))))

Open in new window


Finds the first < for the word(s) before that, and finds last " for word(s) after that.
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 50 total points
ID: 39919374
1. find/replace all ^p with ^l^p  (no wild cards)
2. find/replace all <(*)>*data="<(*)>^l with \1, \2  (with wild cards)

Note: you can manually add the last manual line break at the end in lieu of step 1.
0
 

Author Comment

by:Shirley80
ID: 39919522
Sorry, I am new here.
Since they are so many solutions?
Which solutions should i accept ?
The first solution with the correct answer?
or accept multiple solutions and split the points for expert. thanks
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39919779
If the solutions are the same, the first one.
If the solutions are different, you can split the points.

HTH,
Dan
0
 

Author Closing Comment

by:Shirley80
ID: 39920590
Thanks all for the wonderful answers!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

9 Experts available now in Live!

Get 1:1 Help Now