Extract Text From Variable Length String

Marmit424

Board Regular
Joined
Jul 12, 2016
Messages
58
Hi!


I'm trying to extract text from variable strings. Here is a string example:


id="here_is_an_id" name="here_is_a_name" class="here_is_a_class"


I want to extract the text inside name"". However that text length changes, and the fact that it's not the only item in quotes confuses me on how to use the len,mid,search functions. Would someone please help me accomplish this? Thank you so much!
 
It removed the code again... let's see if this works. The text I'm trying to cut out is inside this >textIwanttocutout<
The problem is this forum's comment processor sees the greater than/less than symbol combination as an HTML tag set. The way around this problem is to put a space immediately after every less than symbol in the text.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Ah that makes perfect sense, thanks! In that case, here is a full example of the code: < a href="//www.youtube.com/yt/dev/" > Developers< /a>. The a will not always be at the end so the only thing to rely on is the > and < then signs. in this example I'd want to extract Developers.
 
Last edited:
Upvote 0
Ah that makes perfect sense, thanks! In that case, here is a full example of the code: < a href="//www.youtube.com/yt/dev/" > Developers< /a>. The a will not always be at the end so the only thing to rely on is the > and < then signs. in this example I'd want to extract Developers.
If the tag starts with "< a href", then why would it not end with the "< /a>" stop tag? What would be at the end of that tag if not "< /a>"?
 
Upvote 0
Sometimes the tag will begin differently (ex: span) and therefore end differently. Good question. I've been thinking over this and the text in between > and < will always be at the end. Therefore, I think I need to start from the right, find the first <. Once found, go from the left and find the second to last > and remove all surrounding text. I don't know how to put that in an equation though although I think it's probably possible.
 
Last edited:
Upvote 0
Some VBA:
Code:
Function GetText(cell)
    With CreateObject("VBScript.RegExp")
        .Pattern = ">(.+?)<"
        With .Execute(cell.Value)
            If .Count > 0 Then
                GetText = .Item(0).SubMatches(0)
            Else
                GetText = CVErr(xlErrNA)
            End If
        End With
    End With
End Function
 
Upvote 0
Some VBA:
Code:
Function GetText(cell)
    With CreateObject("VBScript.RegExp")
        .Pattern = ">(.+?)<"
        With .Execute(cell.Value)
            If .Count > 0 Then
                GetText = .Item(0).SubMatches(0)
            Else
                GetText = CVErr(xlErrNA)
            End If
        End With
    End With
End Function

Thank you! Checking this first thing in the morning!
 
Upvote 0
Unfortunately not, sometimes it will be < span > class="text" name="text" etc and then > textIwant < /span> and sometimes even that span will change.
Just so I fully understand the syntax, the greater than symbol I highlighted in red is a typo, right.... it would not actually be there, (otherwise the greater and less than symbols would be unbalanced), correct?
 
Upvote 0
It's HTML code, isn't it?

< span > is opening syntax
< / span > is closing syntax

Ergo the > to which you refer will be there.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,608
Messages
6,173,325
Members
452,510
Latest member
RCan29

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top