How to find and replace text between two words?


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




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.

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
Who is Participating?
Dan CraciunConnect With a Mentor IT ConsultantCommented:
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
in the find box and
in the replace box.

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

Patrick MatthewsConnect With a Mentor Commented:
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:
Santosh GuptaCommented:

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"
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Dan CraciunIT ConsultantCommented:
@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.
Eric FletcherConnect With a Mentor Commented:
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.
Patrick MatthewsCommented:

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

Ejgil HedegaardConnect With a Mentor Commented:
With a formula in Excel, data in A1

Open in new window

Finds the first < for the word(s) before that, and finds last " for word(s) after that.
aikimarkConnect With a Mentor Commented:
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.
Shirley80Author Commented:
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
Dan CraciunIT ConsultantCommented:
If the solutions are the same, the first one.
If the solutions are different, you can split the points.

Shirley80Author Commented:
Thanks all for the wonderful answers!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.